如何从风格继承并达到深层元素?

时间:2012-05-23 21:24:29

标签: c# wpf xaml inheritance styles

我有一个名为“Home”的MenuItem样式。新的MenuItem-Style“Right”将继承自它。唯一需要覆盖的是样式中Image的ImageSource。最后一个答案中的这个花哨技巧在这里是不可行的,因为样式本身没有ImageSource-Property。

<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="Home" TargetType="{x:Type MenuItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="OnMouseEnter" />
                    </ControlTemplate.Resources>
                    <Grid x:Name="Grid" d:DesignWidth="150" d:DesignHeight="150">
                        <Image x:Name="Image" HorizontalAlignment="Center" VerticalAlignment="Center"
                   Source="/WpfControlLibrary_Battleship;component/Resources/MenuItemBackgrounds/Home.gif" />
                        <Viewbox x:Name="Viewbox">
                            <Label x:Name="Label" Content="{TemplateBinding Header}" />
                        </Viewbox>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="Grid">
                            <BeginStoryboard x:Name="OnMouseEnter_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter}" />
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="Grid">
                            <StopStoryboard BeginStoryboardName="OnMouseEnter_BeginStoryboard" />
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="MinHeight" Value="0" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Background" Value="{x:Null}" />
        <Setter Property="Foreground" Value="{x:Null}" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="Header" Value="" />
    </Style>
    <Style x:Key="Right" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource Home}">

    </Style>
</ResourceDictionary>


最后一个问题及其解决方案:How to inherit from a style and to overwrite something?
(在这种情况下,以下解决方案无法采用。)

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

1 个答案:

答案 0 :(得分:1)

在你的情况下,你为什么不使用MenuItem的Icon属性来设置图像?使用它而不是你的图像:

 <ContentPresenter HorizontalAlignment="Center" 
                      VerticalAlignment="Center"
                      ContentSource="Icon" />

并设置Icon属性

 <Setter Property="Icon">
      <Setter.Value>
        <Image x:Name="Image"  
               Source="/WpfControlLibrary_Battleship;component/Resources/MenuItemBackgrounds/Home.gif" />
      <Setter.Value>
 </Setter>

然后,您可以在继承的样式中轻松更改图像:

<Style x:Key="Right" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource Home}">
   <Setter Property="Icon">
      <Setter.Value>
        <Image x:Name="Image"  
               Source="/WpfControlLibrary_Battleship;component/Resources/MenuItemBackgrounds/Right.gif" />
      <Setter.Value>
 </Setter>
</Style>

作为一般解决方案,或者如果你不能使用Icon属性来解决这个问题,我会移出MenuItem控件,添加我想要的属性并绑定到模板中的该属性。

问候 flusser