Reputation: 95
I made an enum for the state of the program. When it is set for JustInstalled, I am trying to change an XAML label's content using C#.
CS code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace WpfApplication3
{
enum Status
{
JustInstalled,
Configured
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class Program
{
public static void Main()
{
Status status = new Status();
if (!File.Exists("logindata.xml"))
{
status = Status.JustInstalled;
if (status == Status.JustInstalled)
{
Application.Current.Resources["loginlabel"].Content = "Please enter your desired username and password.";
}
}
}
}
}
XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="DFIHUDF" Height="350" Width="525"
>
<Grid>
<Label FontSize="20" x:Name="label" Content="SFDGFDS" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,10,0,0" Width="209"/>
<Label x:Name="loginlabel" Content="x" HorizontalAlignment="Left" Margin="84,95,0,0" VerticalAlignment="Top" Width="89"/>
</Grid>
</Window>
The 2 errors were:
Error CS1061 'object' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) WpfApplication3 C:\Users\Nathan\documents\visual studio 2015\Projects\WpfApplication3\WpfApplication3\MainWindow.xaml.cs 44
and
Error CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. WpfApplication3 C:\Users\Nathan\documents\visual studio 2015\Projects\WpfApplication3\WpfApplication3\obj\Debug\App.g.i.cs 63
I have no idea what the errors are talking about, as I am new to WPF, and my main problem here is that I don't know how to dynamically change the content of a label with c#.
Upvotes: 0
Views: 2904
Reputation: 4892
As I see you do not need to introduce additional stuffs like enums here, The following will sufice:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
if (!File.Exists("logindata.xml"))
StatusText = "Just Installed";
else
StatusText = "Configured";
DataContext = this;
}
public string StatusText {get;set;}
}
Then your label:
<Label x:Name="loginlabel" Content="{Binding StatusText}" />
Upvotes: 4
Reputation: 5892
You need to have a mechanism to update the text when your StatusText property changes. This can be done by using a Dependency Property and binding.
First, put this binding in your XAML:
<Label x:Name="loginlabel" Content="{Binding StatusText}" />
Next, add a dependency property to your code so that the XAML will know about changes:
public static readonly DependencyProperty StatusTextProperty =
DependencyProperty.Register("StatusText", typeof(string), typeof(Window1));
public string StatusText
{
get
{
return this.GetValue(StatusTextProperty) as string;
}
set
{
this.SetValue(StatusTextProperty, value);
}
}
Lastly, add a method to allow for the dynamic setting of the new status:
public void UpdateStatus()
{
if (!File.Exists("logindata.xml"))
{
StatusText = "Please enter your desired username and password.";
}
else
{
StatusText = "Logged In";
}
}
Upvotes: 1
Reputation: 1162
Try this:
public class Program
{
public static void Main()
{
WpfApplication3.MainWindow win = new WpfApplication3.MainWindow();
Status status = new Status();
if (!File.Exists("logindata.xml"))
{
status = Status.JustInstalled;
if (status == Status.JustInstalled)
{
win.loginlabel.Content = "Please enter your desired username and password.";
}
Application.Run(win);
}
}
}
Upvotes: 0