为什么不使用 aDataTrigger
来设置基于RadioButton
选中的样式?
<Style x:Key="MyListViewStyle" TargetType="{x:Type ListView}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=RadioButton1, Path=IsChecked}" Value="True">
<!-- Your Style Setters here -->
<Setter PropertyName="ItemTemplate" Value="{StaticResource ItemTemplate1}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=RadioButton2, Path=IsChecked}" Value="True">
<!-- Your Style Setters here -->
<Setter PropertyName="ItemTemplate" Value="{StaticResource ItemTemplate2}" />
</DataTrigger>
</Style.Triggers>
</Style>
也就是说,将 aListBox
设置为显示为RadioButtons
. 当我想用 显示项目列表RadioButtons
但仍想保持ListBox
选择行为时,我经常这样做。
<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2, 2, 2, 0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent">
<RadioButton
Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
示例使用:
<ListBox ItemsSource="{Binding MyOptions}"
SelectedItem="{Binding SelectedOption}"
Style="{StaticResource RadioButtonListBoxStyle}" />