2016-04-12 39 views
0

我正在尝试构建RadioButton的模板,如选择时发生更改。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上没有这种东西。

如果需要在IsChecked上触发,RadioButton应如何模板化?

回答

1

设置一个名称为边界

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

,这将是从模板访问触发

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

的问题的根源是,单选按钮背景设定明确

Background="{StaticResource CanvasBackgroundColour}" 

当我删除,并在风格设置后台(像这样)

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

触发已经器isChecked没有改变曾在模板

+0

我知道这将是简单的... –