C# ItemsControl重写DefaultStyle
我有一个控件C# ItemsControl重写DefaultStyle,c#,wpf,xaml,C#,Wpf,Xaml,我有一个控件HtNavigationMenuCategoryItem。它在它的构造函数中设置了DefaultStyleKeyDefaultStyleKey=typeof(HtNavigationMenuCategoryItem)。当我没有设置属性OverridesDefaultStyle时,我会得到以下错误“itemTemplate和ItemTemplateSelector对于已经是ItemsControl容器类型的项被忽略;”。但是当我重写它的样式时,它就不起作用了。如果我设定了一种新的风格,
HtNavigationMenuCategoryItem
。它在它的构造函数中设置了DefaultStyleKey
DefaultStyleKey=typeof(HtNavigationMenuCategoryItem)代码>。当我没有设置属性OverridesDefaultStyle
时,我会得到以下错误“itemTemplate和ItemTemplateSelector对于已经是ItemsControl容器类型的项被忽略;
”。但是当我重写它的样式时,它就不起作用了。如果我设定了一种新的风格,那么一切都会成功。我的错误在哪里?如何覆盖样式
风格不起作用
作风
预览使用
如果要为自定义WPF控件提供默认样式,应定义一个调用DefaultStyleKeyProperty的static
构造函数。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" };
}
只要涉及到样式
和控制模板
,它就可以工作
实际上,我有一个“样式工作”解决方案,它有第二个单独的样式,称为“HtNavigationMenuCategoryItemSingle”。但我想知道这是否可能,或者我如何在ItemsControl中重写此样式,就像我在“样式不工作”解决方案中尝试的那样
我想您可以定义ItemContainerStyle
inline:
<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>
谢谢您的回答,但我想知道如何覆盖我的ItemsControl
中的样式,而不是默认样式。HtNavigationMenuCategoryItem
有一个“ObservableCollection”,因此这些类相互交错。第二级应该在ItemsControl中定义“自定义”样式,而不是在ResourceDictionary中定义为public?这可能是做任何你想做的事情的错误方法。我已经添加了我的用法预览。一个类别可以有到站点的直接链接,也可以有内部有直接链接的其他类别(两个独立的集合)。两者(类别和直接链接)应以相同的方式(相同的样式)显示。类别的右侧只有这些箭头(有一个子菜单)。如何重现您的问题?实际上,我有一个“样式工作”解决方案,其中包含第二个单独的样式,名为“HtNavigationMenuCategoryItemSingle”。但是我想知道这是否可能,或者我如何在ItemsControl
中覆盖此Style
,就像我在尝试“Style not working”解决方案一样。。
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 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 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>