How to change control template from Style.Triggers

Raj picture Raj · Oct 7, 2009 · Viewed 10.3k times · Source

I've done it this way:

    <Style x:Key="Button" BasedOn="{StaticResource LoginButton}" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Border CornerRadius="4">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0,1">
                                <GradientStop Offset="0" Color="#0863a5" />
                                <GradientStop Offset="1" Color="#00457d" />
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Border CornerRadius="4">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0,1">
                                        <GradientStop Offset="0" Color="#508fbd" />
                                        <GradientStop Offset="1" Color="#397ab0" />
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Is there any better way to do this? I've scenarios where my control template is having more lines of code, and I'd only require to change a single style like BorderBrush or something. How can I change control template from Style.Triggers efficiently?

Answer

Jobi Joy picture Jobi Joy · Oct 7, 2009

If your intention is just to give a different Background to the Border. You can achieve this in the ControlTemplate.Triggers

<Style x:Key="Button" BasedOn="{StaticResource LoginButton}" TargetType="Button">
  <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate >
        <Grid>
              <Border x:Name="brd" CornerRadius="4">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0,1">
                            <GradientStop Offset="0" Color="#0863a5" />
                            <GradientStop Offset="1" Color="#00457d" />
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
       </Grid>
       <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Background" TargetName="brd">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1">
                     <GradientStop Offset="0" Color="#508fbd" />
                     <GradientStop Offset="1" Color="#397ab0" />
                    </LinearGradientBrush>
                </Setter.Value>
                </Setter>
            </Trigger>
         </ControlTemplate.Triggers>
       </ControlTemplate>
       </Setter.Value>
     </Setter>
 </Style>