如何减少 RadioButton 绑定代码? [英] How can I reduce RadioButton binding code?

查看:23
本文介绍了如何减少 RadioButton 绑定代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 这个关于如何将枚举(在我的情况下为整数)数据绑定到 RadioButtons 的答案,但如果我有几个 TabItem,每个都有 10x10 的 RadioButtons 网格,有什么办法可以摆脱一些样板吗?按原样,每个 RadioButton 都必须包含所有这些信息:

I'm following this answer on how to databind enums (ints in my case) to RadioButtons, but if I've got several TabItems each with 10x10 grids of RadioButtons, is there any way to get rid of some of that boilerplate? As is, each RadioButton has to have all this info with it:

<RadioButton 
    IsChecked="{Binding  
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},  
        Path=FavoriteColor,
        Converter={StaticResource IntToBoolConverter},
        Mode=TwoWay,
        ConverterParameter=5}" 
    Content="Red" Grid.Column="4" Grid.Row="6" />

我希望能够在 TabControl 中设置一次 RelativeSource、Converter 和 Mode,在每个 TabItem 中设置一次 Path,并且只为每个 RadioButton 设置 ConverterParameter.这在 XAML 中可能吗?如果没有,在代码隐藏中进行会更有意义吗?

Preferably I'd like to be able to set the RelativeSource, the Converter, and the Mode once in the TabControl, the Path once in each TabItem, and only have the ConverterParameter set per RadioButton. Is this possible in XAML? If not, would doing it in codebehind make more sense?

推荐答案

这是对我对相关问题的回答的改进,利用ListBoxes的单选模式:

Here would be an improvement on my answer on a related question, utilizing the single selection mode of ListBoxes:

<ListBox SelectionMode="Single" SelectedItem="{Binding EnumValue}"
        Style="{StaticResource BorderlessStyle}">
    <ListBox.Resources>
        <ObjectDataProvider x:Key="items" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:MainWindow+TestEnum" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </ListBox.Resources>
    <ListBox.ItemsSource>
        <Binding Source="{StaticResource items}" />
    </ListBox.ItemsSource>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- Automatic grid layout, adjust as needed -->
            <UniformGrid Columns="2" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding}"
                    IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<小时>

使ListBox自身消失的样式:

<Style x:Key="BorderlessStyle" TargetType="ListBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBox">
                <ItemsPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <ContentPresenter />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

这篇关于如何减少 RadioButton 绑定代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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