Click here to Skip to main content
16,015,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To get I am trying the following code but it the IsSelected property is not getting updated when I select an item in the treeview. Please guide me how to resolve this. Actually my requirement is to do some functionality on selecting an item in the treeview.

XML
<TreeView ItemsSource="{Binding TreeData}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

C#
public class TViewModel : INotifyPropertyChanged
{
    private static object _selectedItem = null;
    // This is public get-only here but you could implement a public setter which also selects the item.
    // Also this should be moved to an instance property on a VM for the whole tree, otherwise there will be conflicts for more than one tree.
    public static object SelectedItem
    {
        get { return _selectedItem; }
        private set
        {
            if (_selectedItem != value)
            {
                _selectedItem = value;
                OnSelectedItemChanged();
            }
        }
    }

    static virtual void OnSelectedItemChanged()
    {
        // Raise event / do other things
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
                if (_isSelected)
                {
                    SelectedItem = this;
                }
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


[edit]code block fixed[/edit]
Posted
Updated 15-Dec-12 7:00am
v2

HTML

Hello
I would approach this by creating a control that inherits TreeView, and has the SelectedItem property with a dependency property so it can be used in XAML code and bindings. Let's call the control MyTreeView.

NOTE: By doing this, I don't kow if the property can be SET, but it can certainly be used to get the selected item.

Here's some example code for the control:
C#
public class MyTreeView : TreeView, INotifyPropertyChanged
{
    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItem", typeof(Object), typeof(MyTreeView), new PropertyMetadata(null));
    public new Object SelectedItem
    {
        get { return (Object)GetValue(SelectedItemProperty); }
        set
        {
            SetValue(SelectedItemsProperty, value);
            NotifyPropertyChanged("SelectedItem");
        }
    }

    public MyTreeView()
        : base()
    {
        base.SelectedItemChanged += new RoutedPropertyChangedEventHandler<Object>(MyTreeView_SelectedItemChanged);
    }

    private void MyTreeView_SelectedItemChanged(Object sender, RoutedPropertyChangedEventArgs<Object> e)
    {
        this.SelectedItem = base.SelectedItem;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String aPropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(aPropertyName));
    }
}


You can now use your control as an ordinary TreeView control, with your alterations.
HTML
<grid>
    <local:mytreeview itemssource="{Binding Path=Items, Mode=OneWay}" xmlns:local="#unknown">
                      SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"
                      Margin="0,0,0,20" />
    <textblock text="{Binding Path=SelectedItem, Mode=OneWay}">
               VerticalAlignment="Bottom" />
</textblock></local:mytreeview></grid>


Hope it helps!
 
Share this answer
 
Hi, Thanks for your reply.

I tried this and I am able to view the Selected item in the dependency property class. But I wan to access this selected item in my view's view model class. Please let me know how to do this.

I tried to implement this by binding it to a property in the viewmodel but itsis not working. Please let me know how to do this.



Thanks in Advance.
 
Share this answer
 
Hi The model class should be the view's DataContext.

In the view class, you can set this either in the constructor by code"
C#
this.DataContext = new ViewClass();


Or in the XAML, in the starting tag:
XML
<viewmodel mode="hold" />                        DataContext="{local:ViewClass}"  />


Now, you can bind form the XAML.
Hope it helped.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900