MVVM:绑定单选按钮视图模型? [英] MVVM: Binding radio buttons to a view model?

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

问题描述

编辑:问题已修复.NET 4.0

Problem was fixed in .NET 4.0.

我一直在试图一组单选按钮的绑定使用器isChecked 按钮视图模型。审查其他职位后,它出现了器isChecked 物业根本不起作用。我已经把一个简短的演示能重现问题,我已经包括在下面。

I have been trying to bind a group of radio buttons to a view model using the IsChecked button. After reviewing other posts, it appears that the IsChecked property simply doesn't work. I have put together a short demo that reproduces the problem, which I have included below.

下面是我的问题:是否有使用MVVM绑定单选按钮直接和可靠的方式?谢谢你。

Here is my question: Is there a straightforward and reliable way to bind radio buttons using MVVM? Thanks.

其他信息:器isChecked 属性不,原因有二:

Additional information: The IsChecked property doesn't work for two reasons:


  1. 在选择一个按键,其他按键的组中的性能器isChecked没有得到设定的

在选择了一个按钮,自有物业器isChecked不会获得首次设置的按钮被选中后。我猜测的结合是越来越在第一次点击通过丢弃WPF。

When a button is selected, its own IsChecked property does not get set after the first time the button is selected. I am guessing that the binding is getting trashed by WPF on the first click.

演示项目:这里是code和标记为一个简单的演示能重现问题。创建一个WPF项目并替换为以下的标记在Window1.xaml:

Demo project: Here is the code and markup for a simple demo that reproduces the problem. Create a WPF project and replace the markup in Window1.xaml with the following:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <StackPanel>
        <RadioButton Content="Button A" IsChecked="{Binding Path=ButtonAIsChecked, Mode=TwoWay}" />
        <RadioButton Content="Button B" IsChecked="{Binding Path=ButtonBIsChecked, Mode=TwoWay}" />
    </StackPanel>
</Window>

替换code在Window1.xaml.cs具有以下code(黑客攻击),这台视图模型:

Replace the code in Window1.xaml.cs with the following code (a hack), which sets the view model:

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = new Window1ViewModel();
        }
    }
}

现在添加以下code以项目为 Window1ViewModel.cs

Now add the following code to the project as Window1ViewModel.cs:

using System.Windows;

namespace WpfApplication1
{
    public class Window1ViewModel
    {
        private bool p_ButtonAIsChecked;

        /// <summary>
        /// Summary
        /// </summary>
        public bool ButtonAIsChecked
        {
            get { return p_ButtonAIsChecked; }
            set
            {
                p_ButtonAIsChecked = value;
                MessageBox.Show(string.Format("Button A is checked: {0}", value));
            }
        }

        private bool p_ButtonBIsChecked;

        /// <summary>
        /// Summary
        /// </summary>
        public bool ButtonBIsChecked
        {
            get { return p_ButtonBIsChecked; }
            set
            {
                p_ButtonBIsChecked = value;
                MessageBox.Show(string.Format("Button B is checked: {0}", value));
            }
        }

    }
}

要重现该问题,运行应用程序,并单击按钮A.一个消息框出现,称巴顿A的器isChecked 属性被设置为的真正的。现在选择按钮B.另一个消息框会出现,称巴顿B的器isChecked 属性被设置为的真正的,但没有消息框,指示该按钮A的器isChecked 属性被设置为的 - 属性并未改变。

To reproduce the problem, run the app and click Button A. A message box will appear, saying that Button A's IsChecked property has been set to true. Now select Button B. Another message box will appear, saying that Button B's IsChecked property has been set to true, but there is no message box indicating that Button A's IsChecked property has been set to false--the property hasn't been changed.

现在再次点击按钮A.该按钮将窗口中选择,但不会显示消息框 - 在器isChecked 属性尚未改变。最后,再次点击B按钮 - 相同的结果。不更新器isChecked 属性在所有的任一按钮按钮被点击第一个后。

Now click Button A again. The button will be selected in the window, but no message box will appear--the IsChecked property has not been changed. Finally, click on Button B again--same result. The IsChecked property is not updated at all for either button after the button is first clicked.

推荐答案

如果你开始与Jason的建议,那么问题就从列表中的单个绑定选择它转换非常漂亮到的ListBox 。在这一点上是微不足道的将样式应用到一个的ListBox 控件,以便它显示为一个单选列表。

If you start with Jason's suggestion then the problem becomes a single bound selection from a list which translates very nicely to a ListBox. At that point it's trivial to apply styling to a ListBox control so that it shows up as a RadioButton list.

<ListBox ItemsSource="{Binding ...}" SelectedItem="{Binding ...}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <RadioButton Content="{TemplateBinding Content}"
                                     IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

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

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