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

查看:155
本文介绍了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天全站免登陆