xaml - How to create two HierarchicalDataTemplates which are mutually-referential in Silverlight? -


Given that:

  1. DataTemplates can not be specified by TargetType
  2. StaticResource reference only reference first

How does the template state the following in a TreeView?

  class processing {public string name {get}} public IEnumerable & lt; Property & gt; Properties {get;}} class property {public string name {get}} public IEnumerable & lt; Resources & gt; Value {get;}}  

This structure lends a good amount of tree which will look like this:

  Resource 1 | - Property A | - Resource 2 | - Resource 3 | - Property B | - Resources 4  

This would be trivial to template in WPF due to TargetType property HierarchicalDataTemplate . In Silverlight, we have to do more:

  & lt; HierarchicalDataTemplate x: Key = "ResourceTemplate" ItemSource = "{Binding Properties}" ItemTemplate = {StaticResource PropertyTemplate} "/> & lt; HierarchicalDataTemplate x: Key =" PropertyTemplate "ItemSource =" {Binding Values} "ItemTemplate =" { StaticResource ResourceTemplate} "/>  

Which clearly can not work because ResourceTemplate can not reference PropertyTemplate because It is defined in the XML document. So, how do you solve this chicken and egg problem?

I have found The only proper solution is to create a third visual model type which forces the first two types (resources or properties) to be given and general properties: (like children). It is still not very favorable, if you have two types Very different templates are required because at that point, I am using VisualStateManager to switch between templates for data.

  & lt; HierarchicalDataTemplate x: key = "tree itam template" madrasource = "{binding child}" & gt; & Lt; Content display content = "{binding}" & gt; & Lt; VisualStateManager.Groups & gt; & Lt; VisualStateGroup & gt; & Lt; Visualstate name = "iSCSI" & gt; & Lt ;! - Set processing template - & gt; & Lt; / VisualState & gt; & Lt; Visualstate name = "isopoprotection" & gt; & Lt ;! - Set properties template - & gt; & Lt; / VisualState & gt; & Lt; / VisualStateGroup & gt; & Lt; /VisualStateManager.Groups> & Lt; / ContentPresenter & gt; & Lt; / HierarchicalDataTemplate & gt;  

Comments