首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemsControl OverridesDefaultStyle

ItemsControl OverridesDefaultStyle
EN

Stack Overflow用户
提问于 2017-03-20 10:23:15
回答 1查看 413关注 0票数 1

我有一个控制HtNavigationMenuCategoryItem。它在it's ConstructorDefaultStyleKey = typeof(HtNavigationMenuCategoryItem);中设置了一个Constructor。当我不设置属性OverridesDefaultStyle时,我会得到以下错误"itemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type;“。但当我推翻它的风格时,它就不起作用了。如果我设定了一种新的风格,那么一切都会成功的。我的错误在哪里?我如何覆盖Style

风格不起作用

代码语言:javascript
复制
<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>

作风工作

代码语言:javascript
复制
 <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>

使用的预览

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-20 11:44:27

如果要为自定义WPF控件提供默认样式,则应定义一个static构造函数,该构造函数调用DefaultStyleKeyProperty.OverrideMetadata方法:

代码语言:javascript
复制
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的文件夹中定义默认样式:

代码语言:javascript
复制
<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>

只要涉及到StyleControlTemplate,就可以做到这一点。

实际上,我有"Style working“解决方案,另一个单独的样式称为"HtNavigationMenuCategoryItemSingle”。但我想知道这是否可能,或者我如何可以在ItemsControl中覆盖这种样式,就像我在尝试"Style not working“解决方案.

我想您可以直接定义ItemContainerStyle内联:

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42901110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档