![](/img/trans.png)
[英]WPF ContextMenu and MenuItem Cannot set OverridesDefaultStyle property in the default Style WPF
[英]ItemsControl OverridesDefaultStyle
我有一個控件HtNavigationMenuCategoryItem
。 它在其Constructor
設置了DefaultStyleKey
; DefaultStyleKey = typeof(HtNavigationMenuCategoryItem);
。 當我不設置Property OverridesDefaultStyle
,出現以下錯誤“ itemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type;
”。 但是當我覆蓋它的樣式時,它不起作用。 如果我設置了新樣式,則一切正常。 我的錯誤在哪里,如何覆蓋Style
?
<Style TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Grid Margin="10,10,10,0">
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CategoryItems}" OverridesDefaultStyle="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Controls:FooControl Text="test" Foreground="Yellow"></Controls:FooControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HtNavigationMenuCategoryItemSingle" TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Controls:FooControl Text="test" Foreground="Yellow"></Controls:FooControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Grid Margin="10,10,10,0">
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CategoryItems}" ItemContainerStyle="{StaticResource HtNavigationMenuCategoryItemSingle}"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果要為自定義WPF控件提供默認的默認樣式,則應定義一個static
構造函數,該構造函數調用DefaultStyleKeyProperty.OverrideMetadata
方法:
public class HtNavigationMenuCategoryItem : ItemsControl
{
static HtNavigationMenuCategoryItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HtNavigationMenuCategoryItem),
new FrameworkPropertyMetadata(typeof(HtNavigationMenuCategoryItem)));
}
public List<string> CategoryItems => new List<string> { "a", "b", "c" };
}
...並在定義自定義控件類的項目根目錄下名為Themes
的文件夾中的ResourceDictionary
Generic.xaml
中定義默認樣式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Navigation="clr-namespace:WpfApplication3">
<Style TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Grid Margin="10,10,10,0">
<StackPanel Orientation="Vertical" Background="Yellow">
<TextBlock>...</TextBlock>
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CategoryItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
只要涉及到Style
和ControlTemplate
, ControlTemplate
可以工作。
實際上,我有一個名為“ HtNavigationMenuCategoryItemSingle”的單獨樣式的“樣式工作”解決方案。 但是我想知道是否有可能或如何像在“樣式無效”解決方案中嘗試的那樣在ItemsControl中覆蓋此樣式...
我想您可以內聯定義ItemContainerStyle
:
<Style TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Grid Margin="10,10,10,0">
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CategoryItems}">
<ItemsControl.ItemContainerStyle>
<Style TargetType="Navigation:HtNavigationMenuCategoryItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem">
<Controls:FooControl Text="test" Foreground="Yellow"></TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.