2012-04-18 34 views
4

Matt Hamilton告訴我一個關於WPF的有趣事實:在4.5版本中,可以使用靜態變量以雙向模式進行綁定。 不幸的是,V4.5仍然是測試版,我決定更改我的代碼以使我的應用程序最終運行正確。雙向綁定在WPF中無法與靜態成員一起工作

但是 - 我仍然有類似的問題,在這裏我們去:

我有一個非常簡單的類「RecallConnectionSettings」。這個類的該成員應當從無處不在的代碼訪問,所以我決定讓他們靜態的(像這樣):

public class RecallConnectionSettings 
    { 
      private static string Server {get;set;} 
    } 

正如你可以看到:只有一個變量「服務器」。 現在我想要的是將2WayMode從TextBox文本屬性綁定到該「服務器」值。

所以,我想這一點:

<UserControl....> 
    <UserControl.Resources> 
      <local:RecallConnectionSettings x:Key="recallConf"/> 
    </UserControl.Resources> 
    <TextBox Text="{Binding Source={StaticResource recallConf}, Path=Server, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ... Name="txtServerAdress" /> 
</UserControl> 

當我改變文本框中的值,這個偉大的工程 - 而不是從另一面。 如果我(手動)更改「服務器」值,我的文本框中的文本屬性將不會更新。

當然不是 - 因爲我現在知道我必須在RecallConnectionSettings類中實現INotifyProperty。 然後,它看起來是這樣的:

public class RecallConnectionSettings : INotifyPropertyChanged 
    { 
    public event PropertyChangedEventHandler PropertyChanged; 
    private static string s_server; 

    public static string Server 
      { 
       get { return s_server; } 
       set 
       { 
        s_server = value; 
        OnPropertyChanged("Server"); 
       } 
      } 

public static event PropertyChangedEventHandler PropertyChanged; 



protected static void OnPropertyChanged(string name) 
      { 
       PropertyChangedEventHandler handler = PropertyChanged; 
       if (handler != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs(name)); 
       } 
      } 
    } 

好 - 這不能工作過。因爲只有靜態方法,我不能使用類的實例調用事件:

PropertyChanged(this, new PropertyChangedEventArgs(name)); 

那麼 - 現在怎麼辦? 我想過使用一個單身,所以我這樣做:

public class RecallConnectionSettings : INotifyPropertyChanged 
    { 
     private static RecallConnectionSettings instance; 

     private RecallConnectionSettings(){} 

     public static RecallConnectionSettings Instance 
     { 
      get 
      { 
       if(instance == null) 
       { 
        instance = new RecallConnectionSettings(); 
       } 
       return instance; 
      } 
     } 
// ... here comes the other stuff 
} 

要使其工作,我也有準備我的用戶,所以我這樣做:

...  
<UserControl.DataContext> 
      <local:RecallConnectionSettings/> 
</UserControl.DataContext> 
... 

在這一點上,不需要繼續嘗試,因爲爲此,默認構造函數必須是公共的。

不管我在做什麼:它不起作用。 在我看來,我仍然不明白這是如何工作的 - 你會如此善良,向我展示這個訣竅?

+2

你不應該從INotifyPropertyChanged的派生? – stijn 2012-04-18 10:25:01

+0

VS中的「輸出 - 調試」顯示了什麼? 您是否在綁定中激活了調試? 像@stijn說,你需要從INotifyPropertyChanged派生 – 2012-04-18 10:27:18

+0

該死的 - 你是對的..我會再試一次。謝謝。 – CodeCannibal 2012-04-18 10:27:38

回答

2

保持單身的解決方案,並替換此:

...  
<UserControl> 
    <UserControl.DataContext> 
     <local:RecallConnectionSettings/> 
    </UserControl.DataContext> 
    ... 
</UserControl> 
... 

通過這樣的:

...  
<UserControl DataContext="{x:Static local:RecallConnectionSettings.Instance}"> 
    ... 
</UserControl> 
... 
+0

實例無法識別... – user1034912 2017-03-23 13:10:56

0

WOW - 感謝尼古拉的作品!

對於其他讀者 - 這裏就是你必須代碼對於現在的文本框:

<TextBox Text="{Binding Path=Server, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="txtServerAdresse"/> 
+0

不起作用,更改時數據不會更新 – user1034912 2017-03-23 13:24:12