I am trying to bind to a static class and can’t seem to get it to work right. Here is what I have for the XAML:
<Window.Resources>
<Game:ActivePlayers x:Key="ActivePlayerInfo" />
</Window.Resources>
<TextBlock x:Name="p1_Name" TextWrapping="Wrap"
Text="{Binding Source={StaticResource ActivePlayerInfo}, Path=PlayerInfo.Player1.Name}"
TextAlignment="Center" FontFamily="Showcard Gothic" VerticalAlignment="Top" />
I can access the ActivePlayerInfo class because if I change the Path to equal “Name” (a temporary dependency property that I created) it works. Here is the code for the ActivePlayerInfo Class:
public class ActivePlayers : DependencyObject
{
public GameInfo PlayerInfo { get { return GameInfo.Singleton; } }
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(ActivePlayers), new UIPropertyMetadata(""));
public ActivePlayers()
{
Name = PlayerInfo.Player1.Name;
}
}
GameInfo.Singleton:
public class GameInfo
{
private static GameInfo gameDetails = new GameInfo();
public static GameInfo Singleton
{
get { return gameDetails; }
}
public PlayerDetails Player1 = new PlayerDetails();
And finally PlayerDetails contains:
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(PlayerDetails), new UIPropertyMetadata("New Player"));
Change your
Player1field to a property.