我正在尝试绑定到RadioButton.IsChecked属性,但它只起作用一次。在那之后,绑定就不起作用了,我也不知道为什么会这样。有人能帮上忙吗?谢谢!
这是我的代码。
C#
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
public class ViewModel
{
private bool _isChecked1 = true;
public bool IsChecked1
{
get { return _isChecked1; }
set
{
if (_isChecked1 != value)
{
_isChecked1 = value;
}
}
}
private bool _isChecked2;
public bool IsChecked2
{
get { return _isChecked2; }
set
{
if (_isChecked2 != value)
{
_isChecked2 = value;
}
}
}
}XAML:
<Grid>
<StackPanel>
<RadioButton Content="RadioButton1" IsChecked="{Binding IsChecked1}" />
<RadioButton Content="RadioButton2" IsChecked="{Binding IsChecked2}" />
</StackPanel>
</Grid>发布于 2010-11-21 08:37:04
这是一个不幸的known bug。我假设这个问题已经在WPF4.0中得到了修复,给出了新的DependencyObject.SetCurrentValue应用程序接口,但还没有得到验证。
发布于 2013-07-20 03:31:30
这里有一个可行的解决方案:http://pstaev.blogspot.com/2008/10/binding-ischecked-property-of.html。令人遗憾的是,微软没有纠正这个错误。
发布于 2015-05-23 02:17:15
只是对Kent的回答的后续,here...this实际上已经在WPF4.0中得到了修复,我在我当前的项目中利用了这个行为。取消激活的单选按钮现在将其绑定值设置为false,而不是断开绑定。
https://stackoverflow.com/questions/4235761
复制相似问题