根据属性改变单选按钮的样式



我有一个RadioButton,它是作为ItemsControl DataTemplate的一部分动态设置的。

<RadioButton GroupName="Ratings">
  <RadioButton.Content>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Score}" />
      <TextBlock Text=" - " />
      <TextBlock Text="{Binding Description}" />
    </StackPanel>
  </RadioButton.Content>
</RadioButton>

我有两个预定义的样式(MyCheckedStyle1MyUncheckedStyle2),当我设置RadioButton的Style=属性时,它们可以单独工作,但我还没有找到一种方法来改变基于它的IsChecked属性的样式。

大多数方法我试图尝试给我一个关于Style object is not allowed to affect the Style property of the object to which it applies.的例外(如ContentTemplate触发器)

伪代码
if IsChecked = true then 
  style = MyCheckedStyle1
else if IsChecked = false then
  style = MyUncheckedStyle1

我可以用代码隐藏来做到这一点,但如果可能的话,我尽量避免这种情况,并将逻辑放在XAML中。

为radio的父类应用一个样式,然后使用触发器通过IsChecked属性改变radio的样式。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="101" Width="264">
    <Window.Resources>
        <Style x:Key="MyCheckedStyle1" TargetType="RadioButton">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style x:Key="MyCheckedStyle2" TargetType="RadioButton">
            <Setter Property="Background" Value="Blue"/>
        </Style>
        <Style x:Key="ParentStyle" TargetType="ContentControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <RadioButton Name="RadioButton1" GroupName="Ratings" >
                            <RadioButton.Content>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Score}" />
                                    <TextBlock Text=" - " />
                                    <TextBlock Text="{Binding Description}" />
                                </StackPanel>
                            </RadioButton.Content>
                        </RadioButton>
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="RadioButton1" Property="IsChecked" Value="True">
                                <Setter TargetName="RadioButton1" Property="Style" 
                                        Value="{StaticResource MyCheckedStyle1}"/>
                            </Trigger>
                            <Trigger SourceName="RadioButton1" Property="IsChecked" Value="False">
                                <Setter TargetName="RadioButton1" Property="Style" 
                                        Value="{StaticResource MyCheckedStyle2}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox>
            <ContentControl Style="{StaticResource ParentStyle}"/>
            <ContentControl Style="{StaticResource ParentStyle}"/>
        </ListBox>
    </Grid>
</Window>

最新更新