Reputation: 356
Evening all,
I'm working on a WPF application which uses databinding to bind variables of a class to a UI. I've set up my class to incorporate INotifyPropertyChanged
which is working fine in my other projects. It currently looks like this:
namespace playerWPF
{
class GameInfo : INotifyPropertyChanged
{
public GameInfo(string _teamName) //constructor
{
this.TeamName = _teamName;
this.OppositionName = "Opposition";
this.TeamScore = 0;
this.OppositionScore = 0;
this.Question = "What is your team name?";
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property));
}
}
private string question;
public string Question
{
get
{
return question;
}
set
{
question = value;
OnPropertyChanged("Question");
}
}
//and so on for all the other variables in a similiar fashion
My XAML looks like this:
<Window x:Class="playerWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:playerWPF"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525"
Name="MainWindowName">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="112*"/>
<RowDefinition Height="207*"/>
</Grid.RowDefinitions>
<TextBox x:Name="answerBox" HorizontalAlignment="Left" Height="32" Margin="58,60,0,0" TextWrapping="Wrap" Text="Answer here..." VerticalAlignment="Top" Width="371" Grid.Row="1"/>
<Button x:Name="submitButton" Content="Submit" Click="submitButton_Click" HorizontalAlignment="Left" Height="35" Margin="199,97,0,0" VerticalAlignment="Top" Width="92" Grid.Row="1"/>
<Label x:Name="teamNameLabel" Content="{Binding TeamName}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="25" Width="74"/>
<Label x:Name="oppositionName" Content="{Binding OppositionName}" HorizontalAlignment="Left" Margin="328,9,0,0" VerticalAlignment="Top" Width="101"/>
<Label x:Name="responseLabel" Content="{Binding Question}" HorizontalAlignment="Left" Height="49" Margin="58,6,0,0" VerticalAlignment="Top" Width="371" Grid.Row="1"/>
<Label x:Name="teamScoreLabel" Content="{Binding TeamScore}" HorizontalAlignment="Left" Margin="19,35,0,0" VerticalAlignment="Top" Width="65"/>
<Label x:Name="oppositionScoreLabel" Content="{Binding OppositionScore}" HorizontalAlignment="Left" Margin="340,40,0,0" VerticalAlignment="Top" Width="65"/>
</Grid>
</Window>
I initialise a type of this class as a static field in my MainWindow
like thus: private static GameInfo gameInfo;
And in the Main window constructor I assign it a new instance: gameInfo = new GameInfo("Team");
It is my understanding that this should change some of the labels on the UI to their default constructed values but this is not the case. Can anyone shed any light?
Upvotes: 0
Views: 1210
Reputation: 11514
You want to utilize gameInfo
like you would a View Model if you were following the MVVM pattern. So, change
DataContext = this; //you don't want the data context to be the window itself
to
DataContext = gameInfo;
Then your bindings are simply
{Binding <property name>}
Upvotes: 2