Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Textblock绑定到静态类_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 将Textblock绑定到静态类

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={

我试图绑定到一个静态类,但似乎无法使它正常工作。以下是我为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"  />
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"));