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

WPF RadTreeView operations using MVVM pattern

5 Answers 654 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
sonal
Top achievements
Rank 1
sonal asked on 17 Nov 2008, 03:42 AM
Hi,
  We are using telerik treeview along with MVVM pattern in a WPF application. The view model exposes an observable collection to which nodes in the tree bound to. Also, viewmodel exposes the commands required to add delete and edit nodes in the tree.

I think, When i add/remove an item in the observable collection it should get reflected in the tree but my concern is, will it preserve the state of expanded tree node after its child nodes are modified?

Also i need to add (drop) a node in the treeview by dragging it from another tree or any other control.

I have few queries:
1. Can you please provide us a sample for add/edit/delete node and drag/drop? It will be great if sample uses MVVM pattern.
2. How theses operations in RadTreeVIew are diffrent/easy as comapred to when i use WPF standard treeview
3.  Can you please shed some light on UI virtualization support in telerik wpf treeview?

Thanks
Sonal

5 Answers, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 21 Nov 2008, 04:47 PM
Hello Sonal,

Thank you for contacting Telerik Support.

Attached you can find an example project where the RadTreeView is bound to an ObservableCollection. Please give it a try.

About virtualization - this is already provided by the framework.

Sincerely yours,
Ivan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
sonal
Top achievements
Rank 1
answered on 24 Nov 2008, 04:27 AM
Hi,
    This sample does not compile, please check.

Thanks
Sonal
0
Ivan
Telerik team
answered on 24 Nov 2008, 08:27 PM
Hi Sonal,

Indeed, the sample we provided is in Silverlight, but there are almost no differences between RadControls for WPF and Silverlight. For example the only difference in attached example is the definition of the HierarchicalDataTemplate class - in WPF it is defined in the framework but in Silverlight it is inside RadControls library.

Attached you can find the same example in WPF. Please recreate the references to the Telerik assemblies in order to compile the code.

Kind regards,
Ivan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
YYZRichard
Top achievements
Rank 2
answered on 30 Jan 2011, 11:34 PM
I often forgot to "RaisePropertyChanged" when I started using MVVM, and that can affect the way the controls behave. You need to notify the property to which the TreeView is binding to, in the code below look for "RaisePropertyChanged".
In the example below, I'm binding the TreeView to an XML Document coming from SQL Server, depending on the node clicked, I have to refresh the content of a Grid, so I retrieve the Grid Content related to the node clicked, and "RaisePropertyChanged" for the property bound to the Grid, that causes the grid to refresh when a tree node is clicked.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
  
    [Export(typeof(FrontlineViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class YourViewModel : NotificationObject
  
        private readonly IEventAggregator eventAggregator;
        private readonly IRegionManager regionManager;
  
        [ImportingConstructor]
        public YourViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            // Event Aggregator
            this.eventAggregator = eventAggregator;
  
            // Region Manager
            this.regionManager = regionManager;
         }
  
         public XmlDocument Tree { get; set; }
         public XmlDocument Grid { get; set; }
  
        /// <summary>
        /// Holds the value of the TreeSelectedItem property.
        /// </summary>
        private System.Xml.XmlElement treeSelectedItem;
  
        /// <summary>
        /// Property to hold the selected item of the TreeView.
        /// </summary>
        public System.Xml.XmlElement TreeSelectedItem
        {
            get { return treeSelectedItem; }
            set
            {
                treeSelectedItem = value;
  
                // Do something here with the SelectedItem
  
                // Raise any properties change, in the example below I'm raising the property used to refresh a grid on the view
                this.RaisePropertyChanged(() => this.Grid);
  
                // You may want to raise this property as well, case another member is dependant on this property.
                this.RaisePropertyChanged(() => this.TreeSelectedItem);
            }
        }
  
}
  
}
0
Petar Mladenov
Telerik team
answered on 02 Feb 2011, 07:26 PM
Hi YYZRichard,

Thank you for your interest in our controls and especially for your feedback. I`m sure the community will appreciate it.

Regards,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
TreeView
Asked by
sonal
Top achievements
Rank 1
Answers by
Ivan
Telerik team
sonal
Top achievements
Rank 1
YYZRichard
Top achievements
Rank 2
Petar Mladenov
Telerik team
Share this question
or