wpf使用Trigger设置RadioButton样式

时间:2016-04-12 14:39:26

标签: c# wpf

我正在尝试为RadioButton构建一个模板,以便在选中时更改。

<RadioButton GroupName="NewTabButons" Content="{Binding TabTitle}"
             Background="{StaticResource CanvasBackgroundColour}"
             Command=... CommandParameter=...>
    <RadioButton.BorderBrush>...</RadioButton.BorderBrush>
    <RadioButton.Template>
        <ControlTemplate TargetType="RadioButton">
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter Property="Control.Background" Value="AliceBlue" />
                    <Setter Property="Control.Foreground" Value="{StaticResource TabTextColourActive}" />
                </Trigger>
            </ControlTemplate.Triggers>
            <Border Height=... Width=... Padding=... VerticalAlignment=...
                    Background="{TemplateBinding Background}"
                    BorderThickness="0 0 0 10"
                    BorderBrush="{TemplateBinding BorderBrush}">
                <Border.Style>
                    <Style BasedOn="{StaticResource Text-Light}" />
                </Border.Style>
                <StackPanel Orientation="Horizontal"
                            VerticalAlignment="Center">
                    <ContentPresenter HorizontalAlignment="Left" 
                                      VerticalAlignment="Center" />
                    <Label Style="{StaticResource TabTextChevron}" />
                </StackPanel>
            </Border>
        </ControlTemplate>
    </RadioButton.Template>
</RadioButton>

选中后,我想更改Border的背景,因为这是构成控件的可视元素。

我已将触发器置于ControlTemplate上,以触发属性IsChecked。但是,这意味着我无法在触发器中设置Border的样式(例如https://stackoverflow.com/a/7965739/4456875)。

如果我将触发器放在边框上,我就无法获得IsChecked属性,因为Border上没有这样的内容。

如果您需要在RadioButton上触发,IsChecked应如何制作?

1 个答案:

答案 0 :(得分:2)

设置边框的名称

<Border x:Name="RadioBorder" Height=... Width=... 

可以从模板触发器访问

<Trigger Property="IsChecked" Value="true">
    <Setter TargetName="RadioBorder" Property="Background" Value="Red"/>
</Trigger>

问题的根源是RadioButton Background明确设置

Background="{StaticResource CanvasBackgroundColour}"

当我删除它并在样式中设置背景(像这样)

<RadioButton.Style>
     <Style TargetType="RadioButton">
         <Setter Property="Background" Value="{StaticResource CanvasBackgroundColour}" />
     </Style>
</RadioButton.Style>

IsChecked的触发器在模板中没有变化

相关问题