This is a migrated thread and some comments may be shown as answers.

retrieve selected node name

1 Answer 296 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
vadim
Top achievements
Rank 1
vadim asked on 06 Apr 2017, 01:27 AM

Hello,

I need to get selected TreeView Node name and bind it to ViewModel property.

I used the following behavior to get SelectedItem from WPF TreeView, but when changed to RadTreeView the SelectedItemChanged  is not recognized. 

Is there any example on ho to get SelectedItem using WPF MVVM ?

 public class TreeViewSelectedItemBlendBehavior : Behavior<RadTreeView>
    {
        //dependency property
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object),
            typeof(TreeViewSelectedItemBlendBehavior),
            new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true });

        //property wrapper
        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (this.AssociatedObject != null)
                this.AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
        }

        private void OnTreeViewSelectedItemChanged(object sender,
            RoutedPropertyChangedEventArgs<object> e)
        {
            this.SelectedItem = e.NewValue;
        }
    }

 

Thank you,

Vadim

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 10 Apr 2017, 08:09 AM
Hello Vadim,

To achieve your requirement you can use the following steps:
  • Define a new property in the main view model that holds the selected item. The property could be of type the view model of the treeview items. Also, the setter of the new property should raise the PropertyChanged event. For example:
    public class MainViewModel : ViewModelBase
    {
        private Node selectedItem; 
        public Node SelectedItem
        {
            get { return selectedItem; }
            set
            {
                if (selectedItem != value)
                {
                    selectedItem = value;
                    OnPropertyChanged("SelectedItem");
                }               
            }
        }
    }
     
    // this is an example view model for the RadTreeViewItems
    public class Node
    {
        // some properties here
    }
  • Then you can bind this property in XAML to the SelectedItem property of RadTreeView using TwoWay binding mode.
    <telerik:RadTreeView SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
Each time you select an treeview item via the UI, the setter of the SelectedItem property in the view model will called. Also, each time you change the SelectedItem property of the view model, if the object is presented in the treeview, the corresponding treeviewitem will be selected. 

Additionally, you can check the Selection article of RadTreeView.

Regards,
Martin
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
TreeView
Asked by
vadim
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or