简体   繁体   中英

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:

 <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
  2. the class is serialized (through datacontract serialization but I think that doesn't change anything).
  3. I restart the program and so the pcdLoggerData.UseBubbleNotifications is automatically set to true 4 the checkbox is not set to TRUE <----- ERROR

point 4 is not correct: since two way I expect to do that automatically.

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. something like 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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