0

我想创建一个逻辑相关的 RadioButtons 列表。RadioButtons 绑定为 MVVM 使用。每个 RadioButtons 上都有 ToolTips。

4

1 回答 1

1

这是一个使用 ListBox 创建一组逻辑相关的 RadioButtons 的 Style。MyClass 包含两个字符串属性:MyName 和 MyToolTip。该样式将显示单选按钮列表,包括正常运行的各个工具提示。这是一个适用于 MVVM 的全绑定、全 Xaml 解决方案。

示例用法:

ListBox Style="{StaticResource radioListBox}" ItemsSource="{Binding MyClasses}" SelectedValue="{Binding SelectedMyClass}"/>

风格:

    <Style x:Key="radioListBox" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="Background" Value="{x:Null}" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid Background="Transparent">
                                <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}" Content="{Binding MyName}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ToolTip" Value="{Binding MyToolTip}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>
于 2010-06-12T15:36:00.567 回答