c# - 如何继承样式并覆盖某些内容?

标签 c# wpf xaml inheritance styles

具体问题是如何继承我的自定义样式“Water”并覆盖它的“Grid.Background”部分?

有可能吗?首先,我只想放另一张图片。

ButtonStyles1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Style x:Key="Water" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="Grid" ShowGridLines="False" d:DesignWidth="150" d:DesignHeight="150">
                        <Grid.Background>
                            <ImageBrush ImageSource="/WpfControlLibrary_Battleship;component/Resources/ButtonBackgrounds/Water.jpg"/>
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="MouseOver" GeneratedDuration="0">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNW"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNE"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSW"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSE"/>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNW">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNE">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSW">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSE">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Path x:Name="PathNW" Data="M2.5,2.5 L82.833332,2.5 M2.5000006,2.5 L2.5000006,82.833329" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathNE" Grid.Column="6" Data="M2.5,2.5 L82.833356,2.5 M82.833333,2.5 L82.833333,82.833335" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathSW" Data="M2.5,2.5 L2.5,82.833328 M2.5,82.833323 L82.833336,82.833323" HorizontalAlignment="Stretch" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathSE" Grid.Column="6" Data="M2.5000001,168.16667 L168.16665,168.16667 M168.16666,2.5000051 L168.16666,168.1667" HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Rectangle x:Name="Rectangle" Grid.ColumnSpan="8" Fill="Red" Margin="0" Grid.RowSpan="8" Stroke="Black" Opacity="0.495">
                            <Rectangle.Visibility>
                                <Visibility>Hidden</Visibility>
                            </Rectangle.Visibility>
                        </Rectangle>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <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="Bow_W" TargetType="{x:Type Button}" BasedOn="{StaticResource Water}">
        <!--How to inherit from the style "Water" and to overwrite the "Grid.Background"-Part?-->
    </Style>
</ResourceDictionary>

最佳答案

您需要将模板绑定(bind)到外部属性,然后您的样式才能覆盖该属性。示例:

<Style x:Key="Water" TargetType="Button">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="/WpfControlLibrary_Battleship;component/Resources/ButtonBackgrounds/Water.jpg"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Grid">
                <Grid Background="{TemplateBinding Background}" ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="SomeOtherWaterStyle" BasedOn="{StaticResource WaterStyle" TargetType="Grid">
    <Setter Property="Background" Value="Red"/>
</Style>

请注意,将哪个属性绑定(bind)到模板的实际选择取决于您。在这种情况下使用 Background 可能是有意义的。

关于c# - 如何继承样式并覆盖某些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10671416/

相关文章:

c# - 如何为绑定(bind)路径传递嵌入式控件的属性?

c# - Linq 查询多行子查询(无连接)

c# - 是否可以使用指向基类型的 AncestorType 进行 WPF RelativeSource 绑定(bind)?

c# - 创建 X.xaml 和 X.xaml.cs 文件 vs 应该理解它们是 xaml 和代码隐藏

c# - BringIntoView 方法

带有可选 GUI 的 C# 类?

c# - 将编码为 UTF-8 的 xml 文件转换为 ANSI

c# - directx/directshow preparesurface 失败/无法呈现图像

c# - WPF ComboboxItem 绑定(bind)

wpf - 如何修复 Visual Studio 2010 中的架构/命名空间错误? (WPF/XAML)