Dr1*_*1Ku 4 c# wpf xaml wpf-controls
有没有办法阻止ItemContainerStyle覆盖已设置的Style(via <Style TargetType="{x:Type MenuItem}">)?
a的样式MenuItem已在ResourceDictionaryXAML文件中定义,该文件在App启动时加载:
<ResourceDictionary>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="{DynamicResource TextForeground}"/>
.. and so on
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
我有以下MenuItemXAML定义.该MenuItem包裹内ContextMenu的通用TextBlock(只是值得一提的我猜的).菜单本身一切顺利,但它的孩子(Enum的实际值)得到了不同的风格,因为ItemContainerStyle它覆盖了它:
<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>
Run Code Online (Sandbox Code Playgroud)
ItemContainerStyle源于我的另一个问题.
MenuItem位于其他图层中,顶层是自定义ContentControl:
public class SomeGradientPanel : ContentControl
{
public SomeGradientPanel ()
{
DefaultStyleKey = typeof(SomeGradientPanel );
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码似乎是问题根源的一个很好的候选者!
因此,完整的结构是:
<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>
Run Code Online (Sandbox Code Playgroud)
我可以参考已定义Style的MenuItem内部ItemContainerStyle吗?样式覆盖仅发生在所述的子MenuItem节点上,父节点具有预期的样式.
谢谢您的意见 !
Fre*_*lad 11
你有没有尝试过
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
Run Code Online (Sandbox Code Playgroud)