在选择IsSelected时更改TreeViewItem模板,并在TreeView中使用两种类型 [英] Change TreeViewItem Template when IsSelected and two types using in TreeView

查看:71
本文介绍了在选择IsSelected时更改TreeViewItem模板,并在TreeView中使用两种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TreeView中,我使用两个不同的类进行绑定.例如,我有一个Group,可以有ChildGroup,也可以有Items. 此类的示例代码:

In my TreeView I use two differnt classes for binding. For example, I have a Group what can have ChildGroup and can have Items. Example code of this classes:

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
public class Group
{
    public Group(string name)
    {
        Name = name;
        items = new ObservableCollection<Item>();
        groups = new ObservableCollection<Group>();
    }
    public string Name { get;
        set;
    }

    private ObservableCollection<Item> items;
    private ObservableCollection<Group> groups;

    public ObservableCollection<Item> Items
    {
        get { return items; }
    }


    public ObservableCollection<Group> Groups
    {
        get { return groups; }
    }

    public IEnumerable<object> AllItems
    {
        get
        {
            foreach (var group in groups)
            {
                yield return group;
            }
            foreach (var item in items)
            {
                yield return item;
            }
        }
    }

}

public class Item
{
    public Item(string name)
    {
        ItemName = name;
    }

    public string ItemName
    {
        get;
        set;
    } 
}
}

要将其绑定到TreeView,我使用以下模板

To bind it to TreeView I use following template

<Grid>
    <TreeView Name="treeView">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}"
                                      ItemsSource="{Binding AllItems}">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type WpfApplication1:Item}">
                <TextBlock Text="{Binding ItemName}" FontStyle="Italic"/>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

很简单.

问题在于,当被选中时,我需要更改ItemTemplate.而且我只需要更改Item类即可.

The problem is that I need to change ItemTemplate when Is selected. And I need to change only then Item class selected.

如果只有一个类用于绑定,我可以做到.使用样式和触发器也很容易,就像这样:

I can do it if only one class use for binding. It also easy using Style and Trigger, like this:

<TreeView Name="treeView1" Grid.Column="1">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}"
                                      ItemsSource="{Binding AllItems}"
                                      x:Key="groupTemplate">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type WpfApplication1:Group}"
                                      ItemsSource="{Binding AllItems}"
                                      x:Key="selectedGroupTemplate">
                <TextBlock Text="{Binding Name}" FontStyle="Italic" FontWeight="Bold" FontSize="14"/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>

        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HeaderTemplate" Value="{StaticResource groupTemplate}"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="HeaderTemplate" Value="{StaticResource selectedGroupTemplate}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>

但是我很难进行多类绑定.

But I have a trouble for multiclass binding.

如何更改SelectedItem模板,然后使用多类绑定?有什么想法吗?

How can I change SelectedItem template then multiclass binding using? Any ideas?

我的示例代码如下:

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
    private ObservableCollection<Group> _groups;
    public ObservableCollection<Group> Groups
    {
        get { return _groups; }
    }

    public Window2()
    {
        InitializeComponent();

        InitGroups();

        treeView.ItemsSource = _groups;
        treeView1.ItemsSource = _groups;
    }

    private void InitGroups()
    {
        _groups = new ObservableCollection<Group>();

        Group group1 = new Group("Group1");
        group1.Groups.Add(new Group("Group1.1"));
        group1.Groups.Add(new Group("Group1.2"));
        group1.Groups.Add(new Group("Group1.3"));

        group1.Items.Add(new Item("Item1.1"));
        group1.Items.Add(new Item("Item1.2"));

        group1.Groups[1].Items.Add(new Item("Item1.2.1"));
        group1.Groups[1].Items.Add(new Item("Item1.2.2"));


        _groups.Add(group1);

        Group group2 = new Group("Group2");
        group2.Groups.Add(new Group("Group2.1"));
        group2.Groups.Add(new Group("Group2.2"));

        group2.Items.Add(new Item("Item2.1"));
        group2.Groups[0].Items.Add(new Item("Item2.1.1"));
        group2.Groups[0].Items.Add(new Item("Item2.1.1"));

        _groups.Add(group2);
    }
}
}

结果

现在我想使用TreeView.HeaderTemplateSelector,但是可能存在仅使用xaml的方式.

Now I think to use TreeView.HeaderTemplateSelector, but may be exists way to use only xaml.

谢谢.

推荐答案

有很多方法可以达到您想要的结果.如果确定DataTemplate仅用于TreeViewItem对象,那么最简单的方法就是直接绑定到TreeViewItem.IsSelected属性,然后对DataTemplate中的更改做出反应:

There are a number of ways to acheive your desired result. If you are sure that your DataTemplate will only be used in TreeViewItem objects, then the easiest is simply to bind directly to the TreeViewItem.IsSelected property and then react to the change in your DataTemplate:

    <DataTemplate DataType="{x:Type WpfApplication1:Item}">
        <TextBlock Text="{Binding ItemName}">
            <TextBlock.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsSelected, RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, FallbackValue=False}" 
Value="True">
                            <Setter Property="TextBlock.FontStyle" Value="Italic" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </DataTemplate> 

这篇关于在选择IsSelected时更改TreeViewItem模板,并在TreeView中使用两种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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