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 クラスに 2 つの依存関係プロパティを作成しました。

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

ツリービューにフィードします。WPF テスト プロジェクトの MainWindow に TreeViewEx のインスタンスを配置します。

<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

問題は、コンテンツをコントロールのインスタンスに設定していて、そのコントロールが親を 1 つしか持てないことです。ツリーを展開して 2 番目のノードに追加すると、最初のノードから削除されます。

ご想像のとおり、コントロールの代わりに DataTemplate を TreeViewEx に提供する必要があります。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 に答える