簡體   English   中英

WPF如何將CheckBox雙向鏈接到類成員和另一個靜態變量

[英]WPF how to bidirectionally link a checkBox to a class member AND to another static variable

我有一個復選框綁定到xaml代碼中的類變量:

 <CheckBox x:Name="cbxUseBubbleNotifications" Margin="20" IsChecked="{Binding Path=pcdLoggerData.UseBubbleNotifications, Mode=TwoWay}"  Content="_Use bubble notifications"  HorizontalAlignment="Left"  VerticalAlignment="Top" Style="{DynamicResource CheckboxSwitchStyle}" />

這應該是雙向綁定,但是發生的是:

  1. 復選框設置為CHECKED ----> var pcdLoggerData.UseBubbleNotifications自動確定
  2. 該類已序列化(通過datacontract序列化,但我認為這沒有任何改變)。
  3. 我重新啟動程序,因此pcdLoggerData.UseBubbleNotifications自動設置為true 4復選框未設置為TRUE <-----錯誤

第4點是不正確的:因為我希望通過兩種方式自動執行此操作。

我的課是:

[DataContract]
public class PCDLoggerBinSerializableData
{
  public PCDLoggerBinSerializableData() { }
  public PCDLoggerBinSerializableData(string _languageInUse, bool _useBubbleNotifications)
  {
   LanguageInUse = _languageInUse;
    UseBubbleNotifications = _useBubbleNotifications;
  }
  [DataMember]
  public string LanguageInUse { get; set; }
  [DataMember]
  public bool UseBubbleNotifications { get; set; }
 }
}

更重要的是,我必須根據pcdLogger.UseBubbleNotifications的相同值/變量設置另一個變量,這是一個STATIC變量。 像Bubble.NoBubbles =!pcdmisData.UseBubbleNotifications

有兩個問題:

  1. 數據綁定不是雙向的(僅一種方式)
  2. 如何對另一個靜態變量進行數據綁定?

謝謝

- 加 -

沒有工作,我在全班同學中都設置了斷點,但從來沒有。

這是我的方法:

 [DataContract]
 public class PCDLoggerBinSerializableData: INotifyPropertyChanged
 {
   #region CONSTRUCTORS
   public PCDLoggerBinSerializableData() { }
   public PCDLoggerBinSerializableData(string _languageInUse, bool _useBubbleNotifications)
   {
     LanguageInUse = _languageInUse;
     UseBubbleNotifications = _useBubbleNotifications;
   }
   #endregion

   #region OPTIONS
   [DataMember]
   public string LanguageInUse { get; set; }
   [DataMember]
   private bool useBubbleNotifications;
   public bool UseBubbleNotifications
   {
     get { return useBubbleNotifications; }
     set
     {
       useBubbleNotifications = value;
       Bubble.NoBubblesPlease = !useBubbleNotifications;
       OnPropertyChange("UseBubbleNotifications");
     }
   }
   #endregion

   #region NOTIFIER
   public event PropertyChangedEventHandler PropertyChanged;
   public void OnPropertyChange(string inName)
   {
     if (PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs("inName"));
   }
   #endregion
  }

就像這樣:

public bool UseBubbleNotifications
{
 get
 {
    return useBubbleNotifications;
 }
 set
 {
    useBubbleNotifications = value;
    Other_Static_Variable = value;
    OnPropertyChange("UseBubbleNotifications");
 }
}

public void OnPropertyChange(string inName)
{
    if(PropertyChanged != null)
      {
         PropertyChanged(this, new PropertyChangedEventArgs("inName"));
      }
}

這樣的事情可能會起作用。 當然,您的類將必須繼承INotifyPropertyChanged接口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM