Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

wpf - How can I reduce RadioButton binding code?

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" />

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

The style to make the ListBox itself disappear:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...