English 中文(简体)
如何继承一种风格 并达到深层元素?
原标题:How to inherit from a style and to reach a deep element?

我有一个名为“家”的菜单项- 玩家。 新的菜单项- 玩家“ 右” 将继承它。 唯一要覆盖的是样式内的图像源。 最后一个答案中的这个花哨在这里是行不通的, 因为一个样式本身没有图像源- 财产 。

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


The last question and its solution: How to inherit from a style and to overwrite something?
(The following solution isn t adoptable in this case.)

<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>
问题回答

在您的情况中, 您为什么不使用菜单项的图标属性来设置图像? 使用此选项代替您的图像 :

 <ContentPresenter HorizontalAlignment="Center" 
                      VerticalAlignment="Center"
                      ContentSource="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>

作为一般解决方案,或者如果您无法使用图标属性解决这个问题,我将会将菜单项目控制程序外移,添加我想要的属性,并在模板中绑定该属性。

regards flusser





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签