ItemsControl 上的 WPF MVVM 单选按钮 [英] WPF MVVM Radio buttons on ItemsControl

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

问题描述

我之前已经将枚举绑定到单选按钮,并且我大致了解它是如何工作的.我使用了这个问题的替代实现:如何绑定单选按钮到枚举?

I've bound enums to radio buttons before, and I generally understand how it works. I used the alternate implementation from this question: How to bind RadioButtons to an enum?

我想生成一个运行时枚举的自定义类型集,而不是枚举,并将它们显示为一组单选按钮.我得到了一个使用 ListView 绑定到 ItemsSourceSelectedItem 属性的运行时枚举集的视图,所以我的 ViewModel 已正确连接.现在我正在尝试从 ListView 切换到带有单选按钮的 ItemsControl.

Instead of enumerations, I'd like to generate a runtime-enumerated set of a custom type and present those as a set of radio buttons. I have gotten a view working against a runtime-enumerated set with a ListView, binding to the ItemsSource and SelectedItem properties, so my ViewModel is hooked up correctly. Now I am trying to switch from a ListView to a ItemsControl with radio buttons.

这是我得到的:

<Window.Resources>
    <vm:InstanceToBooleanConverter x:Key="InstanceToBooleanConverter" />
</Window.Resources>

<!-- ... -->

<ItemsControl ItemsSource="{Binding ItemSelections}">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type vm:ISomeType}">
            <RadioButton Content="{Binding Name}"
                         IsChecked="{Binding Path=SelectedItem, Converter={StaticResource InstanceToBooleanConverter}, ConverterParameter={Binding}}"
                         Grid.Column="0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

InstanceToBooleanConverter 与其他问题中的 EnumToBooleanConverter 具有相同的实现.这似乎是正确的,因为它似乎只是调用了 Equals 方法:

InstanceToBooleanConverter has the same implementation as EnumToBooleanConverter from that other question. This seems right, since it seems like it just invokes the Equals method:

public class InstanceToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

我现在遇到的问题是我不知道如何将运行时值作为 ConverterParameter 发送.当我尝试(使用上面的代码)时,出现此错误:

The problem I am getting now is that I can't figure out how to send a runtime value as the ConverterParameter. When I try (with the code above), I get this error:

无法在绑定"类型的ConverterParameter"属性上设置绑定".只能在 DependencyObject 的 DependencyProperty 上设置绑定".

A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

有没有办法绑定到item实例,并将它传递给IValueConverter?

Is there a way to bind to the item instance, and pass it to the IValueConverter?

推荐答案

事实证明,放弃使用 ItemsControl 而改用 ListBox 要简单得多.

It turns out that it is much simpler to abandon using ItemsControl and instead go with ListBox.

它可能更重,但这主要是因为它为您完成了繁重的工作.在RadioButton.IsCheckedListBoxItem.IsSelected 之间进行双向绑定真的很容易.使用适当的 ListBoxItem 控件模板,您可以轻松摆脱所有选择视觉效果.

It may be more heavy-weight, but that's mostly because it is doing the heavy lifting for you. It is really easy to do a two-way binding between RadioButton.IsChecked and ListBoxItem.IsSelected. With the proper control template for the ListBoxItem, you can easily get rid of all the selection visual.

<ListBox ItemsSource="{Binding Properties}" SelectedItem="{Binding SelectedItem}">
    <ListBox.ItemContainerStyle>
        <!-- Style to get rid of the selection visual -->
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:SomeClass}">
            <RadioButton Content="{Binding Name}" GroupName="Properties">
                <!-- Binding IsChecked to IsSelected requires no support code -->
                <RadioButton.IsChecked>
                    <Binding Path="IsSelected"
                             RelativeSource="{RelativeSource AncestorType=ListBoxItem}"
                             Mode="TwoWay" />
                </RadioButton.IsChecked>
            </RadioButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

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

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