0

I have a TreeView with two levels, parents and children and I would like to get the value of a selected child. I used Josh Smith's TreeView with MVVM pattern to get me started and modified the IsSelected method to get the item that is selected but I'm always getting the parent item.

static object _selectedItem = null;
......
......
public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected = value;
                this.OnPropertyChanged("IsSelected");
                {
                    _selectedItem = this;
                }
            }
        }
    }

Snippet of my XAML:

Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                    <Setter Property="FontWeight" Value="Normal"/>

                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
Gromy
  • 258
  • 1
  • 15
kogilvie
  • 145
  • 2
  • 9
  • 1
    Don't know if this is causing the problem but you're changing `_selectedItem` to current item also when `value` is `false`. Would be good to add `if (value) _selectedItem = this;` – dkozl Aug 19 '13 at 11:47
  • possible duplicate of [SelectedItem in TreeView](http://stackoverflow.com/questions/17676413/selecteditem-in-treeview) – Gayot Fow Aug 19 '13 at 11:57
  • possible duplicate of [Data binding to SelectedItem in a WPF Treeview](http://stackoverflow.com/questions/1000040/data-binding-to-selecteditem-in-a-wpf-treeview) – Ian Ringrose Feb 13 '14 at 17:33

0 Answers0