简体   繁体   中英

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>-->

您可以使用将RelativeSource设置为Border本身来创建Binding ,这允许您绑定到Border自己的BorderBrush属性:

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM