2012-05-23 40 views
0

我有一個名爲「Home」的MenuItem樣式。新的MenuItem-Style「Right」應該從它繼承。唯一要被覆蓋的是樣式中圖像的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

你的情況,你爲什麼不只是使用的圖標屬性MenuItem設置圖像?使用這個,而不是你的形象:

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

作爲一個通用的解決方案或如果你不能使用Icon屬性來解決這個問題,那麼我會去掉MenuItem控件,添加我想要的屬性並綁定到模板中的屬性。

問候 flusser

+0

我需要它作爲一個形象,因爲我要使一些與它同時運行。但沒關係。我今天早上發現了一個(很好?)的解決方案。稍後我會添加它作爲答案。不過謝謝你。 :) – urg0tt

+0

圖標是剛剛命名的圖標,您可以放置​​任何視覺元素。 – flusser