本文介绍了从上位已经设置样式防止ItemContainerStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有防止ItemContainerStyle从覆盖已经设置样式的一种方式(通过<风格的TargetType ={X:类型菜单项}> )为例?

Is there a way of preventing a ItemContainerStyle from overriding an already set Style (via <Style TargetType="{x:Type MenuItem}">) for instance ?

一个对风格菜单项已经在一个 ResourceDictionary中 XAML文件,这是在应用启动时加载:

A style for a MenuItem is already defined within a ResourceDictionary XAML file, which is loaded on App startup :

<ResourceDictionary>
  <Style TargetType="{x:Type MenuItem}">
    <Setter Property="Foreground" Value="{DynamicResource TextForeground}"/>
    .. and so on
  </Style>
</ResourceDictionary>



我有如下的菜单项 XAML定义。在菜单项被包裹在文本菜单的一般的TextBlock (只是值得一提的我猜的)。所有与菜单本身顺利,但其子女(枚举的实际值),得到了不同的风格,因为 ItemContainerStyle 覆盖了它:

I have the following MenuItem XAML definition. The MenuItem is wrapped inside a ContextMenu of a generic TextBlock (just worth mentioning I guess). All goes well with the menu itself, yet its children (the actual values of the Enum) get a different style, since ItemContainerStyle overrides it :

<MenuItem Header="DisplayType" 
          Name="DisplayTypeMenu"
          ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}">

  <MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
      <Setter Property="MenuItem.IsCheckable" Value="True" />

      <Style.Triggers>
        <Trigger Property="MenuItem.Header" 
                 Value="{x:Static enums:DisplayType.Description}" >
            <Setter Property="MenuItem.IsChecked" Value="True" />
        </Trigger>
      </Style.Triggers>

    </Style>
  </MenuItem.ItemContainerStyle>

</MenuItem>



从ItemContainerStyle的的另一个问题。

该菜单项放在其他层中,顶层是一个自定义的ContentControl中:

The MenuItem is placed within other layers, the top layer being a custom ContentControl :

public class SomeGradientPanel : ContentControl
{
    public SomeGradientPanel ()
    {
        DefaultStyleKey = typeof(SomeGradientPanel );
    }
}



上面的代码似乎是为一个很好的候选!?问题的根源

The code above seems to be a good candidate for the source of the problem !?

因此,完整的结构是:

<SomeNameSpace:SomeGradientPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBlock x:Name="SomeLabel">

          <TextBlock.ContextMenu>
              <ContextMenu>
                  <!-- The MenuItem code snippet from above !-->
              </ContextMenu>
          </TextBlock.ContextMenu>

        </TextBlock>

    </Grid>
</SomeNameSpace:SomeGradientPanel>



我可以参考已经定义的风格菜单项 ItemContainerStyle ?样式覆盖只发生在的孩子说菜单项,家长有预期的风格。

Can I refer to the already defined Style for the MenuItem within the ItemContainerStyle ? The Style override only occurs on the children of the said MenuItem, the parent has the expected style.

感谢您您的输入!

推荐答案

你试过

<MenuItem.ItemContainerStyle> 
    <Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">

这篇关于从上位已经设置样式防止ItemContainerStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 14:25