我正在尝试绑定到静态类,似乎无法让它正常工作。以下是我对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" />
我可以访问ActivePlayerInfo类,因为如果我将Path更改为等于“Name”(我创建的临时依赖项属性),它就可以工作。以下是ActivePlayerInfo类的代码:
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();
最后,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"));
答案 0 :(得分:0)
将您的Player1
字段更改为属性。