Question

Je suis juste essayer de trouver un moyen de contrôler l'expansion / effondrement des nœuds TreeView à travers l'objet qu'ils sont liés à. L'objet possède une propriété IsExpanded, et je veux l'utiliser pour montrer le nœud TreeView se développer ou réduire basée sur cette propriété.

Voici mon code:

C #:

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();

        this.DataContext = new List<Parent>() { Base.GetParent("Parent 1"), Base.GetParent("Parent 2") };
    }
}

public class Base
{
    public string Name { get; set; }
    public bool IsExpanded { get; set; }

    public static Parent GetParent(string name)
    {
        Parent p = new Parent() { Name = name };

        p.Children.Add(new Child() { Name = "Child 1", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } });
        p.Children.Add(new Child() { Name = "Child 2", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } });
        p.Children.Add(new Child() { Name = "Child 3", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } });

        return p;
    }
}

public class Parent : Base
{
    public ObservableCollection<Child> Children { get; set; }

    public Parent()
    {
        this.Children = new ObservableCollection<Child>();
    }
}

public class Child : Base
{
    public ObservableCollection<GrandChild> GrandChildren { get; set; }

    public Child()
    {
        this.GrandChildren = new ObservableCollection<GrandChild>();
    }
}

public class GrandChild : Base
{
}

XAML:

<Window x:Class="HeterogeneousExperimentExplorer.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HeterogeneousTree"
    Title="Window2" Height="300" Width="300">
    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding GrandChildren}">
                    <TextBlock Text="{Binding Name}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding}" />
    </Grid>
</Window>
Était-ce utile?

La solution

est venu avec la solution. Vraiment simple:

    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}">
        </Setter>
    </Style>

Alors le style devient l'objet lié à la TreeViewItem et regarde son attribut IsNodeExpanded et il attribue cette valeur à la propriété TreeViewItem.IsExpanded. Si vous ajoutez Mode = TwoWay, ils se sont notifié (TreeViewItem racontera l'objet lorsqu'il a été étendu).

Merci!

Autres conseils

FWIW, vous pouvez être intéressé par cet article CodeProject par Josh Smith qui montre comment créer une arborescence basée MVVM en utilisant une approche générique (n-niveau).

Je ne veux pas dire il n'y a rien de mal avec la mise en œuvre de Carlo, mais je trouve cet article utile pour comprendre le contrôle TreeView et MVVM en général.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top