如何减少RadioButton绑定代码?

Dax*_*ohl 4 data-binding wpf xaml radio-button

我正在按照这个答案来解释如何将枚举(在我的情况下为int)数据绑定到RadioButtons,但如果我有几个TabItem,每个都有10x10的RadioButtons网格,有没有办法摆脱一些样板?按原样,每个RadioButton都必须包含所有这些信息:

<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" />
Run Code Online (Sandbox Code Playgroud)

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

H.B*_*.B. 8

这是对我在相关问题上的答案的改进,利用以下单一选择模式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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)