How to inherit XAML style and override property of child element?

本文探讨了如何在XAML中实现样式继承并覆盖部分属性。通过实例展示了如何定义基本按钮样式,并允许派生类进行样式覆盖。特别介绍了一种方法来改变按钮背景颜色以及如何根据不同状态显示不同图像的切换按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The question


how to inherit XAML style and override some the property of the child element/

the problem

there is a very good question indeed, times are you need to define a base button that you want the derived classs to override. 

 

This article is about the principle tha how you can override the style inherited from the base style.

 

 


the solution

It is more a best practise/guide rather than the solutoin, there is a good artile on on this discussion on the stackoverlow already, so that yoy might have a look here

 


the note "The standard approach to making a customizable control template is to use TemplateBinding in the template to bind to properties  of the control, and then to set those properties in the child styles."

 

 

and here ist one of the example that you can find the stackoverflow forum.

 

 

<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="BaseButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}">
                            <ContentPresenter/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="BlueButtonStyle" TargetType="Button"
               BasedOn="{StaticResource BaseButtonStyle}">
            <Setter Property="Background" Value="Blue"/>
        </Style>
        <Style x:Key="RedButtonStyle" TargetType="Button"
               BasedOn="{StaticResource BaseButtonStyle}">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </StackPanel.Resources>
    <Button Style="{StaticResource RedButtonStyle}">Red</Button>
    <Button Style="{StaticResource BlueButtonStyle}">Blue</Button>
</StackPanel>

 

 

 

 

The note: 

Below is a more elaborated example of how you can overrid the style to make the togglebutton to have different image depending on the IsChecked status.

 

 

<Style x:Key="_icoToggleButton"
           BasedOn="{x:Null}"
           TargetType="{x:Type ToggleButton}">
    <Style.Resources>
      <BitmapImage x:Key="_imageOnSource"
                   UriSource="pack://application:,,,/Resources/Images/excel_icon.gif" />
      <BitmapImage x:Key="_imageOffSource"
                         UriSource="pack://application:,,,/Resources/Images/refresh.PNG" />
    </Style.Resources>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
          <Grid>
            <Image Margin="2,2,2,2"
                           x:Name="image"
                    Source="{DynamicResource _imageOnSource}"
                         />
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="image" Property="Source" Value="{DynamicResource _imageOnSource}" />
                    <Setter Property="ToolTip" Value="Click to Turn Off Auto Sorted/Filtered/Grouped by Fields" />
                  </Trigger>
                  <Trigger Property="IsChecked" Value="False" >
                    <Setter TargetName="image" Property="Source" Value="{DynamicResource _imageOffSource}" />
                    <Setter Property="ToolTip" Value="Click to Turn On Auto Sorted/Filtered/Grouped by Fields" />
                  </Trigger> 
                  <Trigger Property="IsChecked" Value="{x:Null}" >
                    <Setter TargetName="image" Property="Source" Value="{DynamicResource _imageOnSource}" />
                    <Setter Property="ToolTip" Value="Click to Turn On Auto Sorted/Filtered/Grouped by Fields" />
            </Trigger>
            <Trigger Property="IsFocused"
                         Value="True" />
            <Trigger Property="IsMouseOver"
                         Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource _iconOverStoryboard}"
                                     x:Name="_iconOverStoryboard_BeginStoryboard" />
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource _iconAwayStoryboard}"
                                     x:Name="_iconAwayStoryboard_BeginStoryboard" />
              </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsPressed"
                         Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource _iconPressStoryboard}"
                                     x:Name="_iconPressStoryboard_BeginStoryboard" />
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource _iconReleaseStoryboard}"
                                     x:Name="_iconReleaseStoryboard_BeginStoryboard" />
              </Trigger.ExitActions>
            </Trigger>
            <Trigger Property="IsEnabled"
                         Value="False" />
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

 

 

 

Then you can define a child style as such.

 

 

 

<Style x:Key="_customizedIcoToggleButton"
           BasedOn="{StaticResource _customizedIcoToggleButton}"
           TargetType="{x:Type ToggleButton}">
    <Style.Resources>
      <BitmapImage x:Key="_imageOnSource"
                   UriSource="pack://application:,,,/Resources/Images/automatic.gif" />
      <BitmapImage x:Key="_imageOffSource"
                         UriSource="pack://application:,,,/Resources/Images/manua.PNG" />
    </Style.Resources>
    
</Style>

 

 

 

the new style "_customizedIcoToggleButton" now have a different images for on/off status.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值