繁体   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