Update TreeViewMenuItem when ItemsSource changes for HierarchicalDataTemplate

7 posts, 1 answers
  1. OlavXO
    OlavXO avatar
    3 posts
    Member since:
    Aug 2012

    Posted 09 May 2009 Link to this post

    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

  2. Answer
    Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 11 May 2009 Link to this post

    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.
  3. DevCraft banner
  4. OlavXO
    OlavXO avatar
    3 posts
    Member since:
    Aug 2012

    Posted 12 May 2009 Link to this post

    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

  5. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 12 May 2009 Link to this post

    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.
  6. OlavXO
    OlavXO avatar
    3 posts
    Member since:
    Aug 2012

    Posted 12 May 2009 Link to this post

    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

  7. qian
    qian avatar
    1 posts
    Member since:
    May 2011

    Posted 15 May 2011 Link to this post

    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
  8. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 May 2011 Link to this post

    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
Back to Top
DevCraft banner