RadioButton IsChecked绑定未发送取消检查事件 [英] RadioButton IsChecked Binding not sending uncheck event
问题描述
我正在尝试将多个单选按钮数据绑定到某些具有选中属性的对象.如果单击未选择的单选按钮,则将更新我的数据绑定对象.但是,现在不应该检查的另一个对象不是.代码很简单.
I am trying to databind several radiobuttons to some objects that have a checked property. If a click on an unselected radio button, my databound object is updated. However the other object that should now be not checked is not. The code is fairly simple.
我在这里丢失了某些东西吗,还是这是RadioButtons数据绑定中的错误?
Am I missing something here or is this a bug in databinding for RadioButtons?
< Window x:Class = " RadioTest "
xmlns = " http://schemas.microsoft. com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
标题 = " 单选按钮测试 " <字体颜色=#0000ff" size = 2> <字体颜色=#ff0000" size = 2> H八个 = " 154 " 宽度 = " 285 " >
<Window x:Class="RadioTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Radio Button Test" Height="154" Width="285">
< StackPanel 边距 = " 5 " >
<StackPanel Margin="5">
< RadioButton 名称 = " A " 已检查 = " {Binding Path = Checked} " GroupName = " Group " > 是 </ RadioButton >
<RadioButton Name="A" IsChecked="{Binding Path=Checked}" GroupName="Group">Yes</RadioButton>
< RadioButton 名称 = " B " 已检查 = " {Binding Path = Checked} " GroupName = " Group " > 否 </ RadioButton >
<RadioButton Name="B" IsChecked="{Binding Path=Checked}" GroupName="Group">No</RadioButton>
< 复选框 名称 = " C " 已检查 = " {Binding Path = Checked} " > 是 </ 复选框 >
<CheckBox Name="C" IsChecked="{Binding Path=Checked}">Yes</CheckBox>
< 复选框 名称 = " D " 已检查 = " {Binding Path = Checked} " > 否 </ 复选框 >
<CheckBox Name="D" IsChecked="{Binding Path=Checked}">No</CheckBox>
</ StackPanel >
</StackPanel>
</ Window >
</Window>
公共 部分 class RadioTest :System.Windows. Window
public partial class RadioTest : System.Windows.Window
{
MyData _Yes = 新 MyData ();
MyData _Yes = new MyData();
MyData _No = new MyData ();
MyData _No = new MyData();
公共 RadioTest()
public RadioTest()
{
InitializeComponent();
InitializeComponent();
_Yes.Checked = true ;
_Yes.Checked = true;
A.DataContext = _是;
A.DataContext = _Yes;
C.DataContext = _是;
C.DataContext = _Yes;
B.DataContext = _No;
B.DataContext = _No;
D.DataContext = _No;
D.DataContext = _No;
}
}
公共 类 MyData : INotifyPropertyChanged
public class MyData : INotifyPropertyChanged
{
私有 布尔型 _Checked;
private bool _Checked;
公共 布尔型 已选中
public bool Checked
{
获取 { 返回 _Checked; }
get { return _Checked; }
设置
set
{
_Checked = 值 ;
_Checked = value;
OnPropertyChanged( &; Checked"; );
OnPropertyChanged("Checked");
}
}
#region INotifyPropertyChanged成员
#region INotifyPropertyChanged Members
公共 事件 PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler PropertyChanged;
受保护的 无效 OnPropertyChanged( 字符串 propertyName)
protected void OnPropertyChanged(string propertyName)
{
如果 (PropertyChanged!= null )
if (PropertyChanged != null)
{
PropertyChanged( this , 新 PropertyChangedEventArgs (propertyName));
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#endregion
}
}
推荐答案
您需要在绑定上设置Mode = TwoWay.默认值通常为OneWay(除非DP指定BindsTwoWayByDefault),这意味着对元素的更改不会将更改传播回其数据源.
You need to set Mode=TwoWay on your Bindings. The default is typically OneWay (unless a DP specifies BindsTwoWayByDefault) which means changes to the elements won't propagate changes back to their datasource.
HTH,
提请
HTH,
Drew
这篇关于RadioButton IsChecked绑定未发送取消检查事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!