我有一个控制HtNavigationMenuCategoryItem。它在it's Constructor;DefaultStyleKey = typeof(HtNavigationMenuCategoryItem);中设置了一个Constructor。当我不设置属性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>使用的预览

发布于 2017-03-20 11:44:27
如果要为自定义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" };
}...and在定义自定义控件类的项目根目录下的名为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,就可以做到这一点。
实际上,我有"Style working“解决方案,另一个单独的样式称为"HtNavigationMenuCategoryItemSingle”。但我想知道这是否可能,或者我如何可以在ItemsControl中覆盖这种样式,就像我在尝试"Style not working“解决方案.
我想您可以直接定义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>https://stackoverflow.com/questions/42901110
复制相似问题