【发布时间】: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}" />
这应该是双向绑定,但实际情况是:
- 复选框设置为 CHECKED ----> var pcdLoggerData.UseBubbleNotifications 自动确定
- 该类已序列化(通过 datacontract 序列化,但我认为这不会改变任何东西)。
- 我重新启动程序,因此 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
所以有两个问题:
- 数据绑定不能通过两种方式工作(只有一种方式)
- 如何对另一个静态变量进行数据绑定?
谢谢
--添加--
不工作我在课堂的所有部分都设置了断点,但它们从来都不是。
我就是这样做的:
[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