以扩展样式wpf更改控件的属性

本文关键字:控件 属性 扩展 样式 wpf | 更新日期: 2023-09-27 17:51:02

我有一个按钮的样式:

<Style x:Key="ButtonStyle1" TargetType="{x:Type local:ButtonExt}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:ButtonExt}">
            <Grid Name="grid" Margin="0,0,0,0">
               <Rectangle Name="rectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                  <Rectangle.Fill>
                     <LinearGradientBrush StartPoint="0,0" EndPoint="1,0.5">
                        <GradientStop Offset="1" Color="{DynamicResource button_background_gradient1}" />
                        <GradientStop Offset="0" Color="#FF004F96" />
                     </LinearGradientBrush>
                  </Rectangle.Fill>
                  <Rectangle.Effect>
                     <DropShadowEffect BlurRadius="3" Opacity="0.4" ShadowDepth="6" />
                  </Rectangle.Effect>
               </Rectangle>
               <Rectangle Width="0.7" Margin="0,0,43,1" HorizontalAlignment="Right" VerticalAlignment="Stretch" Stroke="#FF434343">
                  <Rectangle.Fill>
                     <LinearGradientBrush StartPoint="0,0" EndPoint="1,0.5">
                        <GradientStop Offset="0" Color="#FFF7F7F7" />
                        <GradientStop Offset="1" Color="#FFD6D6D6" />
                     </LinearGradientBrush>
                  </Rectangle.Fill>
               </Rectangle>
            </Grid>
            <ControlTemplate.Triggers>
               <Trigger Property="IsFocused" Value="True" />
               <Trigger Property="IsDefaulted" Value="True" />
               <Trigger Property="Button.IsPressed" Value="True">
                  <Setter TargetName="grid" Property="Margin" Value="2,2,-1,-1" />
                  <Setter TargetName="rectangle" Property="Effect" Value="{x:Null}"/>
               </Trigger>
               <Trigger Property="IsMouseOver" Value="True"/>
               <Trigger Property="IsPressed" Value="True" />
               <Trigger Property="IsEnabled" Value="False"/>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

和基于此样式的另一个样式:

<Style x:Key="ButtonStyle2" BasedOn="{StaticResource ButtonStyle1}">
    <Setter  Property="Rectangle.Fill">
    <Setter.Value>

在第一种样式中,我有两个矩形。在基于样式1的第二个样式中,我只想更改一个矩形的属性(我将其命名为"rectangle")。我该怎么做呢?

以扩展样式wpf更改控件的属性

ButtonEx类中显示矩形填充的属性。在模板中,使用{TemplateBinding}Fill绑定到这个新属性。在派生样式中,为该属性设置一个新值。