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

Update TreeViewMenuItem when ItemsSource changes for HierarchicalDataTemplate

6 Answers 295 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
OlavXO
Top achievements
Rank 1
OlavXO asked on 09 May 2009, 08:08 PM

We have a problem updating the text for a TreeViewItem when the underlying datasource changes.

Here is our xaml (a bit simplified, removed unrelevant code like the "SystemTemplate"):


<UserControl.Resources> 
 
    <telerikControls:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding System}" ItemTemplate="{StaticResource SystemTemplate}"
            <TextBlock Text="{Binding Name}" /> 
        </telerikControls:HierarchicalDataTemplate> 
 
        <telerikControls:HierarchicalDataTemplate x:Key="ParentTemplate" ItemsSource="{Binding Child}" ItemTemplate="{StaticResource ChildTemplate}"
            <TextBlock Text="{Binding Name}" /> 
        </telerikControls:HierarchicalDataTemplate> 
</UserControl.Resources> 
 
     
<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
    <telerikNavigation:RadTreeView x:Name="rtvTreeMenu" ItemsSource="{Binding Parents}" 
                                       IsLoadOnDemandEnabled="True" 
                                       ItemTemplate="{StaticResource ParentTemplate}" 
                                       LoadOnDemand="TreeMenu_LoadOnDemand" 
                                       ItemPrepared="TreeMenu_ItemPrepared" 
                                       SelectionChanged="TreeMenu_SelectionChanged" 
                                       SelectedValuePath="{Binding Id}"
    </telerikNavigation:RadTreeView> 
... 
 


‘Parents’ is a property of the type ObservableCollection<Parent> in the class the DataContext is bound to, and Child is an ObervableCollection<Child> property in the Parent class.

The user can change the Name property of a Child object (not directly in the TreeView, but another place in the application), thus we need to update the TreeView to show these changes.


We’ve tried to just fire the propertyChanged like this, but nothing happens (although it works when we add a new Child):

propertyChanged(this, new PropertyChangedEventArgs(“Parents”));

Thanks,

Olav

6 Answers, 1 is accepted

Sort by
0
Accepted
Valentin.Stoychev
Telerik team
answered on 11 May 2009, 02:28 PM
Hi Olav Dahl,

Your Parent and Child classes need to implement INotifyPropertyChanged interface in order the changes in them to reflect into the RadTreeView.

Here is a classic example of INotifyPropertyChanged implemention:
public abstract class TaxonomyViewModel : INotifyPropertyChanged  
    {  
        public Taxonomy Taxonomy { get; private set; }  
      
        private bool isExpanded;  
        public bool IsExpanded  
        {  
            get { return isExpanded; }  
            set  
            {  
                isExpanded = value;  
                OnPropertyChanged("IsExpanded");  
            }  
        }  
      
        private bool isSelected;  
        public bool IsSelected  
        {  
            get { return isSelected; }  
            set  
            {  
                isSelected = value;  
                OnPropertyChanged("IsSelected");  
            }  
        }  
        …  
    } 

Please let us know if you have more questions on that.

Regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
OlavXO
Top achievements
Rank 1
answered on 12 May 2009, 07:48 AM

Thanks for leading me in the right direction,


We actually did implement the INotifyPropertyChanged interface for our objects. The problem was that our implementation of it (and my understanding of it) was wrong ;) Anyway we still have a problem when the bound property has a path like SystemType.Name like the one described under:

<telerikControls:HierarchicalDataTemplate x:Key="SystemTemplate" ItemsSource="{Binding Component}" ItemTemplate="{StaticResource ComponentTemplate}"
            <TextBlock Text="{Binding SystemType.Name}" /> 
</telerikControls:HierarchicalDataTemplate> 
 
<telerikControls:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding System}" ItemTemplate="{StaticResource SystemTemplate}"
            <TextBlock Text="{Binding Name}" /> 
</telerikControls:HierarchicalDataTemplate> 

 

But I guess this probably isn't a problem specifically to radTreeView or HierarchicalDataTemplate...


Thanks,

Olav

0
Valentin.Stoychev
Telerik team
answered on 12 May 2009, 10:54 AM
Hi Olav Dahl,

I'm not sure I fully understand your question. Can you elaborate a little bit more on it?

Thanks!

Best wishes,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
OlavXO
Top achievements
Rank 1
answered on 12 May 2009, 08:18 PM

Sorry, there was not really a question in there. Anyway I found the problem and it had nothing to do with the radTreeView. It was just that the SystemType property of the System did not fire propertyChanged (or it did before, but the service reference got updated and erased the manually edited auto-generated file ;), and therefore the radTreeViewItem text was naturally not updated.

 

Olav

0
qian
Top achievements
Rank 1
answered on 15 May 2011, 05:20 PM
Valentin.Stoychev


we can use 

class cls_3
{
     public  <t> rec
     public   IEnumerable < cls_3>  recs
    {
      get ..
    }

}

binding HierarchicalTmplate     ,

How can let the cls_3  :  INotifyPropertyChanged
 and set NotifyPropertyChanged  ?


qian weng
0
Petar Mladenov
Telerik team
answered on 18 May 2011, 01:58 PM
Hello qian,

When binding a collection to a RadTreeView, it`s a good practice that it inherits from ObservaleCollection.
I am attaching a code snippet demonstrating a usual ViewModel class. Please let us know if this is what you need.
public class DataItem
    {
        public DataItem()
        {
            Children = new ObservableCollection<DataItem>();
        }
        public ObservableCollection<DataItem> Children
        {
            get;
            set;
        }
  
        private string header;
        public string Header
        {
            get { return header; }
            set
            {
                header = value;
                RaisePropertyChanged("Header");
            }
        }
  
  
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }               
    }


All the best,
Petar Mladenov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
OlavXO
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
OlavXO
Top achievements
Rank 1
qian
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or