views:

2018

answers:

2

Hello,

Does anyone know how I can get the SelectedItem (not the Header) from a TreeView?
Here is my code:

                <TreeView Name="treeView1" DataContext="{Binding Path=PresentationsViewModel}" Grid.Column="1" >
                    <TreeViewItem IsExpanded="True" Header="Objects-A-List" DisplayMemberPath="Name" ItemsSource="{Binding Path=MyItem.ListA}"></TreeViewItem>
                    <TreeViewItem IsExpanded="True" Header="Objects-B-List" DisplayMemberPath="Name" ItemsSource="{Binding Path=MyItem.ListB}"></TreeViewItem>
                    <TreeViewItem IsExpanded="True" Header="Objects-C-List" DisplayMemberPath="Name" ItemsSource="{Binding Path=MyItem.ListC}"></TreeViewItem>
                </TreeView>

Note that there are 3 different Lists, containing 3 different Object-Types. It'd help me a lot to have something like:

    public Object SelectedObject
    {
        set { _selectedObject = value; RunMyMethod(); RaisePropertyChanged("SelectedObject"); }
    }

Thanks in advance!

Cheers

A: 

Maybe I've misunderstood your question but,

treeView1.SelectedItem

Should work.

Charlie
I'm trying to do this without any Code-Behind, just via <TreeView Name="treeView1" DataContext="{Binding Path=PresentationsViewModel}" Grid.Column="1" SelectedItem="{Binding Path=MySelectedObject}">Unfortunately TreeView doesn't have this (but ListView for example does have one)
Joseph Melettukunnel
A: 

Ok I know this is an old question and probably dead but as Charlie has it right. This is something that can also be used in code. You could do for example:

<ContentPresenter Content="{Binding ElementName=treeView1, Path=SelectedItem}" />

Wich will show the selected item. You can add a style or DataTemplate to that or use a default DataTemplate to the object you are trying to show.

Ingó Vals