locked
HierarchicalDataTemplate in code? RRS feed

  • Question

  • Hi,

    What is the equivalent in code-behind of this xaml resourece (note that I don't specify a Key, only the DataType)?

                <HierarchicalDataTemplate ItemsSource="{Binding}" 
                                          DataType="{x:Type local:CountryList}">  
                    <TextBlock Text="{Binding Name}"></TextBlock> 
                </HierarchicalDataTemplate> 
     

    I have this so far, but am not sure how far this is correct.
    HierarchicalDataTemplate hdt = new HierarchicalDataTemplate(typeof(CountryList));  
    hdt.VisualTree = new FrameworkElementFactory(typeof(TextBlock));  
    hdt.VisualTree.Text = "Countries";  
    hdt.ItemsSource = new Binding();  
                  
    this.Resources.Add(?, hdt); 

    You see I am not sure how to add the template without a key (requriement).
    And am not sure if I have translated the ItemsSource property setting and the Text property setting into code-behind.

    Any help would be much apprecialted.

    Thanks
    Thursday, August 28, 2008 8:48 PM

Answers

  • You can use a DataTemplateKey to store the resource:

     
    HierarchicalDataTemplate hdt = new HierarchicalDataTemplate(typeof(CountryList));  
    hdt.ItemsSource = new Binding();  
    FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));  
    tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));  
    hdt.VisualTree = tb;  
    Resources[new DataTemplateKey(typeof(CountryList))] = hdt;  
     

    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Edited by Dr. WPF Thursday, August 28, 2008 10:18 PM clarify
    • Proposed as answer by Dr. WPF Thursday, August 28, 2008 10:18 PM
    • Marked as answer by Vandhana Friday, August 29, 2008 8:15 PM
    Thursday, August 28, 2008 9:29 PM

All replies

  • You can use a DataTemplateKey to store the resource:

     
    HierarchicalDataTemplate hdt = new HierarchicalDataTemplate(typeof(CountryList));  
    hdt.ItemsSource = new Binding();  
    FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));  
    tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));  
    hdt.VisualTree = tb;  
    Resources[new DataTemplateKey(typeof(CountryList))] = hdt;  
     

    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Edited by Dr. WPF Thursday, August 28, 2008 10:18 PM clarify
    • Proposed as answer by Dr. WPF Thursday, August 28, 2008 10:18 PM
    • Marked as answer by Vandhana Friday, August 29, 2008 8:15 PM
    Thursday, August 28, 2008 9:29 PM
  • Thanks Dr., that worked!

    Also, I wanted to specify a hard-coded string for the TextBlock (the sample xaml i provided was wrong) like this:

    tb.Text = "Something";

    But, that blows up on me with this error message:

    >>>>>>>>>>>
    System.InvalidOperationException was unhandled
      Message="'ContentPresenter' type must implement IAddChild to be used in FrameworkElementFactory AppendChild."
      Source="PresentationFramework"
      StackTrace:
           at System.Windows.FrameworkElementFactory.InstantiateTree(UncommonField`1 dataField, DependencyObject container, DependencyObject parent, List`1 affectedChildren, List`1& noChildIndexChildren, FrugalStructList`1& resourceDependents)
    ............
    >>>>>>>>>>>

    Any ideas why this might be happening?

    Thanks
    Friday, August 29, 2008 2:32 PM
  • To set a DP via an element factory, you need to use SetValue():

     
    tb.SetValue(TextBlock.TextProperty, "Something");  
     

    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Proposed as answer by Dr. WPF Friday, August 29, 2008 6:46 PM
    Friday, August 29, 2008 6:45 PM
  • Thanks, that worked!

    I am surprised I missed the SetValue API.
    Friday, August 29, 2008 8:16 PM