当TreeViewItem被选中且在TreeView中使用两种类型时,更改TreeViewItem的模板。

4

在我的TreeView中,我使用两种不同的类来进行绑定。例如,我有一个组,可以有ChildGroup和Items。 这些类的示例代码如下:

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

<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。而且只有选定的项类需要更改。

如果只使用一个类进行绑定,我可以做到这一点。使用Style和Trigger也很容易,就像这样:

<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>

但是我在多类绑定方面遇到了麻烦。

如果要使用多类绑定,我该如何更改SelectedItem模板?有什么想法吗?

我的示例代码:

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的方法。

谢谢。


我相信你使用模板选择器是正确的方向。我也希望有一种仅使用XAML的解决方案来处理这种情况。我猜在XAML中运行任意代码的能力太过复杂了。我看到过一些解决方案,比如这个,但我还没有尝试过。 - Paul Hoenecke
@Victor Chekalin -- 你有没有找到解决办法?这是一个很好的问题,我也卡在了同样的问题上。 - Rachael
1个回答

5

有许多方法可以达到您想要的结果。如果您确定您的 DataTemplate 仅在 TreeViewItem 对象中使用,则最简单的方法是直接绑定到 TreeViewItem.IsSelected 属性,然后在您的 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> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接