本文介绍了"ItemTemplate"和"ItemTemplateSelector"均已设置; "ItemTemplateSelector"将被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题之后,我还有另一个问题TreeView.

Following this question, I have another question about TreeView.

我已经拥有的是带有HierarchicalDataTemplateTreeView,在其中可以更改level2的HierarchicalDataTemplate(如问题和答案中所述).

What I already have is a TreeViewwith HierarchicalDataTemplate, in which I can change the HierarchicalDataTemplate of level2 (like explained in the question and the answer).

我现在想要的是更改Treeview扩展器的外观.为此,我已经定义了一个名为ctForTreeViewItemControlTemplate,并按如下方式使用它:

What I want now, is to change the look of the expander of the Treeview. For this, I have defined a ControlTemplate named ctForTreeViewItem, and I use it like this:

<Window.Resources>
    <ControlTemplate x:Key="ctForTreeViewItem"
                     TargetType="{x:Type TreeViewItem}">
        <Expander IsExpanded="True"
                  Background="Grey"
                  BorderBrush="Transparent"
                  Foreground="White"
                  BorderThickness="1,1,1,3">
            <Expander.Header>
                <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                        Padding="{TemplateBinding Control.Padding}"
                        BorderBrush="{TemplateBinding Border.BorderBrush}"
                        Background="{TemplateBinding Panel.Background}"
                        Name="Bd"
                        SnapsToDevicePixels="True"
                        Grid.Column="1">
                    <ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}"
                                      ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                      ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                                      ContentSource="Header"
                                      Name="PART_Header"
                                      HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </Border>
            </Expander.Header>

            <Expander.Content>
                <ItemsPresenter x:Name="ItemsHost" />
            </Expander.Content>

        </Expander>
    </ControlTemplate>

    <DataTemplate x:Key="Level3Template">
        <Border Background="LightBlue">
            <TextBlock Text="Level3"/>
        </Border>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="Level2RedTemplate"
                          ItemsSource="{Binding Value}"
                          ItemTemplate="{StaticResource Level3Template}">
        <Border Background="Red">
            <TextBlock Text="Level2"/>
        </Border>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="Level2YellowTemplate"
                          ItemsSource="{Binding Value}"
                          ItemTemplate="{StaticResource Level3Template}">
        <Border Background="Yellow">
            <TextBlock Text="Level2"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="Level1Template" 
                          ItemsSource="{Binding Value}"
                          ItemTemplateSelector="{StaticResource MySelector}">

        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="Template"
                        Value="{StaticResource ctForTreeViewItem}" />
            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>

        <Border Background="Green">
            <TextBlock Text="Level1"/>
        </Border>
    </HierarchicalDataTemplate>
</Window.Resources>

<TreeView Grid.Row="1"
          Name="tv"
          ItemsSource="{Binding Items}"
          ItemTemplate="{StaticResource Level1Template}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="Template"
                    Value="{StaticResource ctForTreeViewItem}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

这按预期适用于Level1,因此在Level 1上,我具有TreeViewItems,其中已定义ControlTemplate和正确的HierarchicalDataTemplate.

This works for Level1 as expected, so on Level 1, I have TreeViewItems with the defined ControlTemplate and the Correct HierarchicalDataTemplate.

但是,它在我有ItemTemplateSelector的Level2上不起作用.在这个位置,我得到这个错误:System.Windows.Data Error: 25 : Both 'ItemTemplate' and 'ItemTemplateSelector' are set; 'ItemTemplateSelector' will be ignored.

However, it doesn't work on Level2, where I have an ItemTemplateSelector. At this position, I get this error: System.Windows.Data Error: 25 : Both 'ItemTemplate' and 'ItemTemplateSelector' are set; 'ItemTemplateSelector' will be ignored.

  • 有什么办法可以在保持ItemTemplateSelector的同时将ControlTemplate分配给TreeViewItem吗?甚至

  • Is there any way, that I can assign a ControlTemplate to the TreeViewItems, while keeping the ItemTemplateSelector? Or even

还有其他方法可以更改TreeView的扩展器的样式吗?

Is there any other way, that I can change the style of the expander of the TreeView?

推荐答案

您的TreeViewItem ControlTemplate已损坏.它会忽略数据模板选择器,因为您通过在ContentPresenter 上设置ContentTemplate显式覆盖它.这也与在调试输出流中导致(无害)错误的原因类似:2级模板从其祖先那里继承了一个ItemTemplateSelector,就像您在TreeView上设置一次ItemContainerStyle一样.将被树视图的所有子级继承,除非某些中间子级显式覆盖它.无需多次设置.由于2级模板继承了ItemTemplateSelector并用其他属性覆盖它,因此会出现错误,但这是无害的.

Your TreeViewItem ControlTemplate is broken. It ignores the data template selector because you explicitly override that by setting ContentTemplate on the ContentPresenter. That's similar to what's causing the (harmless) errors in your debug output stream as well: The level 2 templates inherit an ItemTemplateSelector from their ancestors, in the same way that if you set ItemContainerStyle once on the TreeView, it will be inherited by all children of the treeview unless some intervening child overrides it explicitly. No need to set it more than once. Because the level 2 templates inherit ItemTemplateSelector and override it with a different property, you get an error, but it's harmless.

我通过更改Header的ContentPresenter来匹配默认的TreeViewItem控件模板,从而修复了控件模板:我删除了ContentContentTemplateContentStringFormat属性. ContentPresenter具有所有这些内容的默认行为,因此您无需显式指定它.

I fixed the control template by changing the ContentPresenter for the Header to match the default TreeViewItem control template: I removed the Content, ContentTemplate, and ContentStringFormat attributes. The ContentPresenter has default behavior for all that stuff so you don't need to specify it explicitly.

<ContentPresenter 
    ContentSource="Header"
    Name="PART_Header"
    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
    />

模板/模板选择器错误是无害的,但我能够通过将2级分层数据模板上的ItemTemplateSelector显式设置为null来使它们静音:

The template/template selector errors are harmless but I was able to silence them by setting ItemTemplateSelector explicitly to null on the level 2 hierarchical data templates:

<HierarchicalDataTemplate 
    x:Key="Level2RedTemplate"
    ItemsSource="{Binding Value}"
    ItemTemplateSelector="{x:Null}"
    ItemTemplate="{StaticResource Level3Template}"
    >
    <Border Background="Red">
        <TextBlock Text="Level2"/>
    </Border>
</HierarchicalDataTemplate>

这篇关于"ItemTemplate"和"ItemTemplateSelector"均已设置; "ItemTemplateSelector"将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:13