如何在TreeViewItem的模板上使用DataTemplateSelector? [英] How to use DataTemplateSelector on TreeViewItem's template?

查看:66
本文介绍了如何在TreeViewItem的模板上使用DataTemplateSelector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TreeView ,并为其 TreeViewItems 模板提供了自定义资源字典.

I have a TreeView with a custom resource dictionary for its TreeViewItems template.

我需要能够根据其绑定值以编程方式更改每个 TreeViewItem 的模板.

I need to be able to programatically change the template for each TreeViewItem based on its binding's value.

据我所知,我无法从 ResourceDictionary xaml访问绑定.

As far as I can tell, I can't access the binding from the ResourceDictionary xaml.

我认为我需要使用自己的userControl扩展 TreeView ,但是我不确定要实现自己想要的功能需要实际添加什么内容.

I think I need to extend the TreeView with my own userControl, but I'm not sure what I need to actually add to it to achieve what I want.

谢谢.

<TreeView Name="dirTree" AlternationCount="2" Background="#FAFAFA"
                       ScrollViewer.VerticalScrollBarVisibility ="Visible"
              VerticalAlignment="Stretch"
              ItemsSource="{Binding}"
              VirtualizingStackPanel.IsVirtualizing="False"
              VirtualizingStackPanel.VirtualizationMode="Standard"
              ItemTemplateSelector="{StaticResource tuningDataTemplateSelector}">
            <TreeView.Resources>
                <ResourceDictionary Source="UI/Styles/TunerTreeViewStyle.xaml" />
            </TreeView.Resources>

        </TreeView>

这是树视图,您可以看到我设置了ItemTemplateSelector,但这仅更改了内容.我需要为TreeViewItem控件模板本身添加选择器.因此,我需要将其添加到TunerTreeViewStyle.xaml,但我认为您不能从那里获取绑定数据.

This is the treeview, and as you can see I have an ItemTemplateSelector set, but this only changes the contents. I need to add a selector for the TreeViewItem control's template itself. So I would need to add it to the TunerTreeViewStyle.xaml but I don't think you can get the bound data from there.

推荐答案

您不能使用 ItemTemplateSelector 更改 TreeViewItem 的 ControlTemplate 代码>.

You can't use an ItemTemplateSelector to change the ControlTemplate of a TreeViewItem.

如果要执行此操作,可以尝试使用设置了模板的 Template 属性的 DataTrigger 定义 ItemContainerStyle 基于某些源属性的TreeViewItem ,例如:

If you want to do this you could try to define an ItemContainerStyle with a DataTrigger that sets the Template property of the TreeViewItem based on some source property, e.g.:

<TreeView>
    <TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding YourProperty}" Value="A">
                    <Setter Property="Template" Value="{StaticResource ResourceA}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding YourProperty}" Value="B">
                    <Setter Property="Template" Value="{StaticResource ResourceB}" />
                </DataTrigger>
                <!-- ... -->
            </Style.Triggers>
        </Style>
    </TreeView.Resources>
</TreeView>

这篇关于如何在TreeViewItem的模板上使用DataTemplateSelector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆