我有一个控件HtNavigationMenuCategoryItem
。它已设置DefaultStyleKey
Constructor
; DefaultStyleKey = typeof(HtNavigationMenuCategoryItem);
。如果我未设置属性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>
答案 0 :(得分:2)
如果要为自定义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" };
}
...并在项目根目录中名为ResourceDictionary
的文件夹中定义名为Generic.xaml
的{{1}}中的默认样式,其中定义了自定义控件类:
Themes
这涉及到<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
。
实际上我有&#34; Style工作&#34;具有第二个单独样式的解决方案称为&#34; HtNavigationMenuCategoryItemSingle&#34;。但我想知道它是否可能,或者我如何在ItemsControl中覆盖此样式,就像我在&#34;样式不工作&#34;解决方案...
我猜你可以定义内联ControlTemplate
:
ItemContainerStyle