Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change control template from Style.Triggers

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?

like image 903
Raj Avatar asked Oct 07 '09 04:10

Raj


1 Answers

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>
like image 139
Jobi Joy Avatar answered Oct 27 '22 06:10

Jobi Joy