WPF复选框IsChecked属性不会根据绑定值进行更改

编程入门 行业动态 更新时间:2024-10-21 14:36:59
本文介绍了WPF复选框IsChecked属性不会根据绑定值进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这里是我的代码:

xaml side:我使用数据模板绑定项dataType1

< DataTemplate DataType ={x:Type dataType1}> < WrapPanel> < CheckBox IsChecked ={Binding Path = IsChecked,Mode = TwoWay}Command ={Binding Path = CheckedCommand} /> < TextBlock Text ={Binding Path = ItemName,Mode = OneWay}/> < / WrapPanel> < / DataTemplate>

然后我创建一个类型为dataType1的项的ComboBox

< ComboBox Name =comboBoxItemsItemsSource = {Binding Path = DataItems,Mode = TwoWay}>

这里是dataType1 diffinition: / p>

class dataType1 {public string ItemName {get; set;} public bool IsChecked {get; set;}}

该场景是我准备一个dataType1的列表,并将其绑定到ComboBox,ItemName显示无瑕疵,而CheckBox IsChecked值始终未被选中不管dataType1中的IsChecked的值如何。

在wpf中复选框中绑定IsChecked属性时需要特殊处理吗?

彼得·乐ung

解决方案

您在这里遇到的问题是 CheckBox 不知道 dataType1.IsChecked 的值何时更改。要修复它,请将您的dataType1更改为:

class dataType1:INotifyPropertyChanged { public string ItemName {得到;组; private bool isChecked; public bool IsChecked { get {return isChecked; set { if(isChecked!= value) { isChecked = value; if(PropertyChanged!= null) { PropertyChanged(this,new PropertyChangedEventArgs(IsChecked)); } } } } public event PropertyChangedEventHandler PropertyChanged; }

所以现在,当属性值更改时,它会通知绑定它需要通过提高 PropertyChanged 事件来更新。

此外,还有更简单的方法可以避免你写得尽可能多的锅炉代码。我使用 Josh Smith的BindableObject 。

here is my code:

xaml side: I use a data template to bind with item "dataType1"

<DataTemplate DataType="{x:Type dataType1}"> <WrapPanel> <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Command="{Binding Path=CheckedCommand} /> <TextBlock Text="{Binding Path=ItemName, Mode=OneWay}" /> </WrapPanel> </DataTemplate>

then I create a ComboBox with item with type "dataType1"

<ComboBox Name="comboBoxItems" ItemsSource="{Binding Path=DataItems, Mode=TwoWay}">

and here is dataType1 difinition:

class dataType1{public string ItemName{get; set;} public bool IsChecked {get; set;}}

the scenario is I prepare a list of dataType1 and bind it to the ComboBox, ItemName display flawlessly while CheckBox IsChecked value is always unchecked regardless the value of "IsChecked" in dataType1.

Is special handling needed in binding IsChecked property in CheckBox in wpf?

Peter Leung

解决方案

The problem you're having here is that the CheckBox doesn't know when the value of dataType1.IsChecked changes. To fix that, change your dataType1 to:

class dataType1 : INotifyPropertyChanged { public string ItemName { get; set; } private bool isChecked; public bool IsChecked { get { return isChecked; } set { if (isChecked != value) { isChecked = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); } } } } public event PropertyChangedEventHandler PropertyChanged; }

So now, when the property value is changed it will notify the binding that it needs to update by raising the PropertyChanged event.

Also, there are easier ways to do this that avoid you having to write as much boiler-plate code. I use BindableObject from Josh Smith.

更多推荐

WPF复选框IsChecked属性不会根据绑定值进行更改

本文发布于:2023-11-11 23:28:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   属性   定值   WPF   IsChecked

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!