RezaNoei
RezaNoei

Reputation: 1479

Set a property value took from existing property WPF ControlTemplate

I'm working on WPF ControlTemplates. I want to say whenever the mouserover event was fired on my Buttons, their background colors turn to their borderBrush Color.

 <Style TargetType="Button">
     <Setter Property="Cursor" Value="Hand"/>
     <Setter Property="Background" Value="White"/>
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="Button">
                 <Grid>
                     <Border x:Name="Border" CornerRadius="6" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                         <TextBlock x:Name="ContentBlock" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}" VerticalAlignment="Center"  HorizontalAlignment="Center"></TextBlock>
                     </Border>
                 </Grid>
                 <ControlTemplate.Triggers>
                     <Trigger Property="IsEnabled" Value="false">
                         <Setter TargetName="Border" Property="BorderBrush" Value="Gray"></Setter>
                         <Setter TargetName="ContentBlock" Property="Foreground"  Value="Gray"></Setter>
                     </Trigger>
                     <Trigger Property="IsMouseOver" Value="true">
                         <!--<Setter TargetName="Border" Property="Background" Value="Border.BorderBrush"></Setter>-->
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>

in below line, i have tried to take borderBrush from my Button. but i can't do this way:

 <!--<Setter TargetName="Border" Property="Background" Value="Border.BorderBrush"></Setter>-->

Upvotes: 0

Views: 175

Answers (2)

R.A 1
R.A 1

Reputation: 127

I think It is no easy solution to set reference value in the trigger. Is it an issue if you set the value with a colour, the way you are setting for the background.

    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" 
     Property="BorderBrush" Value="Gray"></Setter>
                            <Setter TargetName="ContentBlock" 
    Property="Foreground"  Value="Gray"></Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" 
    Property="Background" Value="Gray" />
                        </Trigger>
                    </ControlTemplate.Triggers>

Upvotes: 0

V.Leon
V.Leon

Reputation: 586

You can create Binding with RelativeSource set to the Border itself, which allows you to bind to Border's own BorderBrush property:

<Setter TargetName="Border" Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />

Upvotes: 1

Related Questions