下面是一个通过使用列表框创建一组逻辑相关单选按钮的样式。MyClass包含两个字符串属性:MyName和MyToolTip。样式将显示单选按钮列表,包括功能正常的单个工具提示。这是一个供MVVM使用的全绑定、全Xaml解决方案。
用法示例:
ListBox Style=“{StaticResource radioListBox}”ItemsSource=“{Binding MyClasses}”SelectedValue=“{Binding selectedyclass}”/>
<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>