vinmm
vinmm

Reputation: 297

Selection & identification of child node in TreeView

I have created a treeview in my xaml.

<TreeView Name="exportTreeView" ItemsSource="{Binding}" Width="350" >
<TreeView.Resources>
    <DataTemplate x:Key="layersTemplate">
        <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
            <CheckBox Foreground="White" IsChecked="{Binding IsToBeExported}" VerticalAlignment="Center" />
            <Label Style="{StaticResource baseStyle}" Content="{Binding Path=Name}" VerticalAlignment="Center" />
        </StackPanel>
    </DataTemplate>
    <HierarchicalDataTemplate x:Key="objectTemplate" ItemsSource="{Binding Path=LayersList}" ItemTemplate="{StaticResource ResourceKey=layersTemplate}">
        <StackPanel Orientation="Horizontal" Height="15" Margin="10,0,0,0">
            <CheckBox Foreground="White" IsChecked="{Binding IsToBeExported}" VerticalAlignment="Center" />
            <Label Style="{StaticResource baseStyle}" Content="{Binding Path=Name}" VerticalAlignment="Center" />
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=ObjectList}" ItemTemplate="{StaticResource ResourceKey=objectTemplate}">
        <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
            <CheckBox Foreground="White" IsChecked="{Binding IsToBeExported}" VerticalAlignment="Center" />
            <Label Style="{StaticResource baseStyle}" Content="{Binding Path=Name}" VerticalAlignment="Center" />
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

The tree structure is like below. Each Parent can have any number of children & each Child can have any number of Grandchildren. Multiple selection is allowed too.

Parent
  -Child
    --Grandchild

I have checkboxes for all levels. I am not getting how to access its nodes individually and also how to use the tree data.

In my VM class, I set the datacontext of this TreeView to a 3 class list like below:

public class MProject
{
    public string Name { get; set; }
    public bool IsToBeExported { get; set; }
    public List<MWorkObject> ObjectList { get; set; }
}

public class MWorkObject
{
    public string Name { get; set; }
    public bool IsToBeExported { get; set; }
    public List<MLayer> LayersList { get; set; }
}

public class MLayer
{
    public string Name { get; set; }
    public bool IsToBeExported { get; set; }
}

My requirement is:

  1. Selecting the parent should select all its child and grandchild.
  2. How to identify in the code which item is selected ? Need it to do further processing.

Please help.

Upvotes: 0

Views: 449

Answers (1)

Rekshino
Rekshino

Reputation: 7325

You need to implement INotifyPropertyChanged for your classes. Then

  1. in setter of IsToBeExported in MProject handle all children (set IsToBeExported to what you need). The binding makes the change visible in tree
  2. if IsToBeExported set to true, then it is selected

Example:

public class ViewBase :INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {       
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
    }
}

public class MProject : ViewBase
{
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                NotifyPropertyChanged(nameof(Name));
            }
        }
    }
    private string _name;

    ...
}

Upvotes: 1

Related Questions