简体   繁体   English

WPF如何将CheckBox双向链接到类成员和另一个静态变量

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

I have a checkbox which is binded to a class variable in the xaml code: 我有一个复选框绑定到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}" />

this should be supposed to be a two way binding but what happen is: 这应该是双向绑定,但是发生的是:

  1. the checkbox is set to CHECKED ----> the var pcdLoggerData.UseBubbleNotifications is automatically OK 复选框设置为CHECKED ----> var pcdLoggerData.UseBubbleNotifications自动确定
  2. the class is serialized (through datacontract serialization but I think that doesn't change anything). 该类已序列化(通过datacontract序列化,但我认为这没有任何改变)。
  3. I restart the program and so the pcdLoggerData.UseBubbleNotifications is automatically set to true 4 the checkbox is not set to TRUE <----- ERROR 我重新启动程序,因此pcdLoggerData.UseBubbleNotifications自动设置为true 4复选框未设置为TRUE <-----错误

point 4 is not correct: since two way I expect to do that automatically. 第4点是不正确的:因为我希望通过两种方式自动执行此操作。

My class is: 我的课是:

[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; }
 }
}

Even more important I have to set another variable according to the same value/variations of pcdLogger.UseBubbleNotifications and that is a STATIC var. 更重要的是,我必须根据pcdLogger.UseBubbleNotifications的相同值/变量设置另一个变量,这是一个STATIC变量。 something like Bubble.NoBubbles = !pcdmisData.UseBubbleNotifications 像Bubble.NoBubbles =!pcdmisData.UseBubbleNotifications

So two problems: 有两个问题:

  1. databinding not TWO-WAY working (only one way) 数据绑定不是双向的(仅一种方式)
  2. how to databind also another static var? 如何对另一个静态变量进行数据绑定?

Thanks 谢谢

--ADD-- - 加 -

Not working I put breakpoints in all parts of the class and they never were it. 没有工作,我在全班同学中都设置了断点,但从来没有。

This is how I did it: 这是我的方法:

 [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
  }

It would be something like: 就像这样:

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"));
      }
}

Something like this may work. 这样的事情可能会起作用。 Of course your class would have to inherit the INotifyPropertyChanged interface. 当然,您的类将必须继承INotifyPropertyChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM