MVVM 单选按钮 [英] MVVM radiobuttons

查看:21
本文介绍了MVVM 单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮忙.我有一个有趣的问题.我正在尝试实现一个 MVVM 应用程序,并且我想绑定到我认为的单选按钮.

Someone please help. I have an interesting issue. I am trying to implement an MVVM app and I want to bind to radiobuttons in my view.

这是我的观点:

<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>

这是我的视图模型:

    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 时,只有前两次单击更新我的视图模型.我想我的绑定可能有问题,或者我可能以完全错误的方式处理这个问题.

The problem is this, when I click the different radiobuttons the property setter only gets called the first time(when i run thru debugging). e.g. When I click NoteGeneral, NoteContact, then NoteGeneral again only the first two clicks update my viewmodel. I think I might have something wrong with my binding, or maybe I'm approaching this the completely wrong way.

有人可以帮忙吗?

我应该如何在我的视图模型中实现单选按钮选择?

How should I implement radiobutton selections in my viewmodel?

Microsoft 在 .NET 4 发布时解决了 RadioButton 绑定的这个问题.RadioButtons 的绑定现在可以如您所愿,无需下面列出的任何变通方法.

This issue with RadioButton binding was resolved by Microsoft when .NET 4 was released. Binding of RadioButtons now works as you would expect without any of the work-arounds listed below.

推荐答案

看一看 这里.

我还没有实施提供的解决方案,但它是有道理的.执行单击时,底层框架控件会破坏您的绑定.解决方案是覆盖执行此操作的方法并仅依赖绑定.

I haven't implemented the solution provided but it makes sense. The underlying framework control breaks you bindings when a click is performed. The solution is to override the method that does this and just rely on the bindings.

这篇关于MVVM 单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆