retrieve selected node name

2 posts, 0 answers
  1. vadim
    vadim avatar
    4 posts
    Member since:
    Sep 2014

    Posted 05 Apr Link to this post

    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

  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    1407 posts

    Posted 10 Apr Link to this post

    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.
Back to Top