4
有沒有辦法阻止ItemContainerStyle覆蓋已設置的樣式(通過<Style TargetType="{x:Type MenuItem}">
)?阻止ItemContainerStyle覆蓋已設置的樣式
<ResourceDictionary>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="{DynamicResource TextForeground}"/>
.. and so on
</Style>
</ResourceDictionary>
我有以下MenuItem
XAML定義:
樣式的MenuItem
已經是一個ResourceDictionary
XAML文件,它在App啓動時加載內定義。 MenuItem
被包裝在TextBlock
的一個ContextMenu
裏面(值得一提的是我猜)。所有與菜單本身順利,但其子女(枚舉的實際值),得到了不同的風格,因爲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>
的ItemContainerStyle從another question of mine莖。
的菜單項被置於其它層內,頂層是一個自定義ContentControl中:
public class SomeGradientPanel : ContentControl
{
public SomeGradientPanel()
{
DefaultStyleKey = typeof(SomeGradientPanel);
}
}
上面的代碼看起來是問題的根源的良好候選!?
因此,完整的結構是:
<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
內MenuItem
定義Style
?樣式覆蓋僅出現在所述MenuItem
的子代中,父代具有預期樣式。
謝謝您的輸入!
是的,但是這使得與以前相比(例如缺少文/頭)的更基本的風格顯示的孩子。再次感謝您的輸入! – Dr1Ku 2010-12-13 16:18:11
@ Dr1Ku:當然可以:)我無法重現這一點。您是否有任何其他資源可能導致隱式MenuItem樣式被應用?像PresentationFramework.Aero或app.xaml中的東西? – 2010-12-13 16:32:13
@ Dr1Ku:如果您刪除了ItemContainerStyle的Style,它的工作正常嗎? – 2010-12-13 16:38:04