有人可以帮忙吗?我遇到了一个有趣的问题。我正在尝试实现一个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绑定的问题。现在单选按钮的绑定可以像你期望的那样工作,无需使用下面列出的任何解决方法。