2011-05-09 66 views
0

我想綁定到一個靜態類,似乎無法讓它正常工作。以下是我對XAML:將Textblock綁定到靜態類

<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類。下面是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

「似乎無法讓它正常工作」 - 您期望的行爲是什麼,您看到的行爲是什麼? – 2011-05-09 04:37:21

回答

0

Player1字段更改爲一個屬性。

+0

這樣做!萬分感謝! – Stacey 2011-05-09 14:45:48