繁体   English   中英

ItemsControl重写DefaultStyle

[英]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>

只要涉及到StyleControlTemplateControlTemplate可以工作。

实际上,我有一个名为“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM