6

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Raj
  • 4,405
  • 13
  • 59
  • 74

1 Answers1

5

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>
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
  • Oops! I didn't know ControlTemplate too have Triggers. Cool :-) – Raj Oct 07 '09 at 06:58
  • 8
    what if he wanted an completely different template for his control ? – eran otzap Jun 25 '12 at 16:13
  • @eran otzap This is exactly *not* the question. We don't want to be forced to replace the whole Template to only change a few properties. – Paul Apr 18 '19 at 08:59
  • @Paul you are correct.. seems weird that i would answer something else. Could be that in the last 9 year this question was edited ? – eran otzap Apr 18 '19 at 10:20