【问题标题】:WPF how to bidirectionally link a checkBox to a class member AND to another static variableWPF如何将复选框双向链接到类成员和另一个静态变量
【发布时间】:2016-01-13 11:44:30
【问题描述】:

我有一个复选框,它绑定到 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
  }

【问题讨论】:

  • 对于您的第一个问题,您是否尝试过使用 INotifyPropertyChanged?我认为您必须这样做才能让视图知道您已在代码中对其进行了更新。对于您的第二个问题,您如何拥有一个绑定到两个不同变量的复选框?如果这些变量不同怎么办?复选框应该显示哪个值?如果这些值始终相同,那么您可以在 UseBubbleNotifications 设置器属性的设置器中设置第二个值吗?
  • 不,我不想请你告诉我如何回答?至于第二个问题?谢谢
  • 不确定这是否有帮助,但我收到此错误:

标签: c# wpf checkbox data-binding class-members


【解决方案1】:

应该是这样的:

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 接口。

【讨论】:

  • System.Windows.Data 错误:17:无法从“”(类型“MainViewModel”)获取“pcdLoggerData”值(类型“对象”)。 BindingExpression:路径=pcdLoggerData.UseBubbleNotifications; DataItem='MainViewModel' (HashCode=63617293);目标元素是'CheckBox'(名称='cbxUseBubbleNotifications');目标属性是 'IsChecked'(类型 'Nullable`1') InvalidOperationException:'System.InvalidOperationException: Invalid Path propertyPercorso della proprietà non valido。
  • 对于“System.Dynamic.DynamicObject+MetaDynamic”,没有一个名为“Items”的公共属性。在 MS.Internal.DynamicPropertyAccessorImpl.GetValue(Object component) 中的 MS.Internal.Data 中的 System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,Tret](CallSite site, T0 arg0) 中的 CallSite.Target(Closure , CallSite , Object ) 中。 MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'中的PropertyPathWorker.GetValue(Object item, Int32 level)
  • 你的数据上下文设置是什么?
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 2015-11-01
  • 2011-02-11
相关资源
最近更新 更多