KyloRen
KyloRen

Reputation: 2741

TreeView MVVM, how to get selection?

I am trying to create a TreeView for my application. This is the first time I am using TreeView with the MVVM structure, by all accounts the binding is working and displaying correctly.

However:

How do I get the selection so I can perform some logic after the user selects something?

I thought that the TextValue property in SubSection class would fire PropertyChanged, but it doesn't, so I am left scratching my head.


This is the most simplified set of code I could make for this question:

Using PropertyChanged setup like this in : ViewModelBase class

public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

The VeiwModel:

public class ShiftManagerViewModel : ViewModelBase 
{

    public ShiftManagerViewModel()
    {
        Departments = new List<Department>()
        {
            new Section("Section One"),
            new Section("Section Two")
        };
    }

    private List<Section> _sections;

    public List<Section> Sections
    {
        get{return _sections;}
        set
        {
            _sections = value;
            NotifyPropertyChanged();
        }
    }
}

The classes:

public class Section : ViewModelBase
{
    public Section(string depname)
    {
        DepartmentName = depname;
        Courses = new List<SubSection>()
        {
            new SubSection("SubSection One"),
            new SubSection("SubSection One")
        };
    }

    private List<SubSection> _courses;
    public List<SubSection> Courses
    {
        get{ return _courses; }
        set
        {
            _courses = value;
            NotifyPropertyChanged();
        }
    }
    public string DepartmentName { get; set; }
}

public class SubSection : ViewModelBase
{
    public SubSection(string coursename)
    {
        CourseName = coursename;
    }
    public string CourseName { get; set; }

    private string _vTextValue;

    public string TextValue
    {
        get { return _vTextValue; }
        set
        {
            _vTextValue = value;
            NotifyPropertyChanged();
        }
    }
}

And the XAML:

<Window.Resources>
     <HierarchicalDataTemplate ItemsSource="{Binding Courses}" DataType="{x:Type viewModels:Section}">
        <Label Content="{Binding DepartmentName}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate ItemsSource="{Binding TextValue}" DataType="{x:Type viewModels:SubSection}">
        <Label Content="{Binding CourseName}" />
    </HierarchicalDataTemplate>
</Window.Resources>

Could someone point me in the right direction?

Upvotes: 2

Views: 1634

Answers (1)

mm8
mm8

Reputation: 169160

You could cast the SelectedItem property of the TreeView to a Section or a SubSection or whatever the type of the selected item is:

Section section = treeView1.SelectedItem as Section;
if (section != null)
{
    //A Section is selected. Access any of its properties here
    string name = section.DepartmentName;
}
else
{
    SubSection ss = treeView1.SelectedItem as SubSection;
    if(ss != null)
    {
        string ssName = ss.CourseName;
    }
}

Or you could add an IsSelected property to the Section and SubSection types and bind the IsSelected property of the TreeView to it:

<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</TreeView.ItemContainerStyle>

Then you get the selected item by iterating through the ItemsSource of the TreeView and look for the item that has the IsSelected source property set to true.

Upvotes: 1

Related Questions