C# 将Textblock绑定到静态类
我试图绑定到一个静态类,但似乎无法使它正常工作。以下是我为XAML准备的:C# 将Textblock绑定到静态类,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我试图绑定到一个静态类,但似乎无法使它正常工作。以下是我为XAML准备的: <Window.Resources> <Game:ActivePlayers x:Key="ActivePlayerInfo" /> </Window.Resources> <TextBlock x:Name="p1_Name" TextWrapping="Wrap" Text="{Binding Source={
<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" />
GameInfo.Singleton:
public class GameInfo
{
private static GameInfo gameDetails = new GameInfo();
public static GameInfo Singleton
{
get { return gameDetails; }
}
public PlayerDetails Player1 = new PlayerDetails();
最后,PlayerDetails包含:
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"));
将您的
Player1
字段更改为属性。“似乎无法使其正常工作”--您期望的行为是什么,以及您看到的行为是什么?
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"));