【发布时间】:2010-10-27 08:42:42
【问题描述】:
请有人帮忙。我有一个有趣的问题。我正在尝试实现一个 MVVM 应用程序,并且我想在我的视图中绑定到单选按钮。
这是我的看法:
<StackPanel Orientation="Horizontal" Grid.ColumnSpan="2" >
<RadioButton GroupName="1" IsChecked="{Binding Path=NoteGeneral, Mode=TwoWay}">General</RadioButton>
<RadioButton GroupName="1" IsChecked="{Binding Path=NoteContact, Mode=TwoWay}" >Contact</RadioButton>
<RadioButton GroupName="1" IsChecked="{Binding Path=NoteAddress, Mode=TwoWay}" >Address</RadioButton>
<RadioButton GroupName="1" IsChecked="{Binding Path=NotePhone, Mode=TwoWay}" >Phone</RadioButton>
</StackPanel>
这是我的 ViewModel:
bool _NoteGeneral;
public bool NoteGeneral
{
get { return _NoteGeneral; }
set
{
_NoteGeneral = value;
OnPropertyChanged("NoteGeneral");
}
}
bool _NoteContact;
public bool NoteContact
{
get { return _NoteContact; }
set
{
_NoteContact = value;
OnPropertyChanged("NoteContact");
}
}
bool _NoteAddress;
public bool NoteAddress
{
get { return _NoteAddress; }
set
{
_NoteAddress = value;
OnPropertyChanged("NoteAddress");
}
}
bool _NotePhone;
public bool NotePhone
{
get { return _NotePhone; }
set
{
_NotePhone = value;
OnPropertyChanged("NotePhone");
}
}
问题是这样的,当我单击不同的单选按钮时,属性设置器只会在第一次被调用(当我通过调试运行时)。例如当我再次单击 NoteGeneral、NoteContact 和 NoteGeneral 时,只有前两次单击会更新我的视图模型。我想我的绑定可能有问题,或者我正在以完全错误的方式处理这个问题。
谁能帮忙?
我应该如何在我的视图模型中实现单选按钮选择?
.NET 4 及更高版本
当 .NET 4 发布时,Microsoft 解决了 RadioButton 绑定的这个问题。 RadioButtons 的绑定现在可以正常工作,而无需下面列出的任何解决方法。
【问题讨论】:
标签: data-binding mvvm