2

我需要构建一个自定义树视图作为用户控件。为了示例 TreeViewEx ,我将其称为:

<UserControl x:Class="WpfApplication4.TreeViewEx"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="root">
    <Grid>
        <TreeView ItemsSource="{Binding Path=ItemsSource, ElementName=root}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Node : "/>
                        <ContentControl Content="{Binding Path=AdditionalContent, ElementName=root}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</UserControl>

这个想法是拥有 ItemTemplate 内容的固定部分和可自定义的部分。

当然,我在 TreeViewEx 类上创建了两个依赖属性:

public partial class TreeViewEx
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
        "ItemsSource", typeof(IEnumerable), typeof(TreeViewEx));

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty AdditionalContentProperty = DependencyProperty.Register(
        "AdditionalContent", typeof(object), typeof(TreeViewEx));

    public object AdditionalContent
    {
        get { return GetValue(AdditionalContentProperty); }
        set { SetValue(AdditionalContentProperty, value); }
    }

    public TreeViewEx()
    {
        InitializeComponent();
    }
}

有一个像这样的简单节点类:

public class Node
{
    public string Name { get; set; }
    public int Size { get; set; }
    public IEnumerable<Node> Children { get; set; }
}

我会喂树视图。我将 TreeViewEx 的一个实例放在 WPF 测试项目的 MainWindow 上:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication4">
    <Grid>
        <local:TreeViewEx x:Name="tree">
            <local:TreeViewEx.AdditionalContent>
                <TextBlock Text="{Binding Name}"/>
            </local:TreeViewEx.AdditionalContent>
        </local:TreeViewEx>
    </Grid>
</Window>

最后喂它:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();

        var dummyData = new ObservableCollection<Node>
        {
            new Node
            {
                Name = "Root", 
                Size = 3,
                Children = new ObservableCollection<Node>
                {
                    new Node{
                        Name="Child1",
                        Size=2,
                        Children = new ObservableCollection<Node>{
                            new Node{
                                Name = "Subchild", 
                                Size = 1
                            }

                        }
                    }

                }
            }
        };

        tree.ItemsSource = dummyData;
    }
}

但是它没有按预期工作,即起初 ContentControl 有数据,但是当我展开节点时,它不显示 ContentControl 的内容。

我真的不明白..我应该使用DataTemplate还是其他东西?

4

1 回答 1

2

问题是您将内容设置为控件的实例,并且该控件只能有一个父级。当您展开树并将其添加到第二个节点时,它会将其从第一个节点中删除。

正如您所怀疑的,您想为 TreeViewEx 提供一个 DataTemplate 而不是控件。您可以使用 ContentPresenter 在树的每个级别实例化模板:

将 AdditionalContentProperty 替换为:

public static readonly DependencyProperty AdditionalContentTemplateProperty = DependencyProperty.Register(
    "AdditionalContentTemplate", typeof(DataTemplate), typeof(TreeViewEx));

public DataTemplate AdditionalContentTemplate
{
    get { return (DataTemplate)GetValue(AdditionalContentTemplateProperty); }
    set { SetValue(AdditionalContentTemplateProperty, value); }
}

将 UserControl 的 XAML 中的 HierarchicalDataTemplate 更改为:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Node : "/>
    <ContentPresenter ContentTemplate="{Binding Path=AdditionalContentTemplate, ElementName=root}"/>
</StackPanel>

并将 MainWindow 更改为:

<local:TreeViewEx x:Name="tree">
    <local:TreeViewEx.AdditionalContentTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </local:TreeViewEx.AdditionalContentTemplate>
</local:TreeViewEx>
于 2010-07-06T23:08:43.040 回答