Silverlight Hierarchical template for Multiple Types in Treeview
-
public class TreeItem { public string HeaderTag { get; set; } public List<TreeStatusItem> StatusItems { get; set; } public List<TreeItem> items { get; set; } public TreeItem() { StatusItems = new List<TreeStatusItem>(); } } public class TreeStatusItem { #region Properties [DataMember] public string StatusText { get; set; } [DataMember] public string StatusValue { get; set; } } Here a TreeItem has a HaderTag which displays as the Header for the treeview, the StatusItems contains items of a different type and the items have a List of the container class itself. The sample data would be -RegionName(object of TreeItem type) --Oustanding(object of TreeStatusItem type) --Live (object of TreeStatusItem type) --Areas(object of TreeItem type) ----Oustanding(object of TreeStatusItem type) ----Live (object of TreeStatusItem type) ----SubAreas(object of TreeItem type) ...... ... I need to bind the data which is List<TreeItem> to a silverlight treeview. I can`t figure out how the hierarchical template should be !!!!
When you fail to plan, you are planning to fail.
-
public class TreeItem { public string HeaderTag { get; set; } public List<TreeStatusItem> StatusItems { get; set; } public List<TreeItem> items { get; set; } public TreeItem() { StatusItems = new List<TreeStatusItem>(); } } public class TreeStatusItem { #region Properties [DataMember] public string StatusText { get; set; } [DataMember] public string StatusValue { get; set; } } Here a TreeItem has a HaderTag which displays as the Header for the treeview, the StatusItems contains items of a different type and the items have a List of the container class itself. The sample data would be -RegionName(object of TreeItem type) --Oustanding(object of TreeStatusItem type) --Live (object of TreeStatusItem type) --Areas(object of TreeItem type) ----Oustanding(object of TreeStatusItem type) ----Live (object of TreeStatusItem type) ----SubAreas(object of TreeItem type) ...... ... I need to bind the data which is List<TreeItem> to a silverlight treeview. I can`t figure out how the hierarchical template should be !!!!
When you fail to plan, you are planning to fail.
I create a class ReportNode, it uses the NodeTag property to store the actual record that is used to populate the treenode
public class ReportNode : ViewModelBase { public ReportNode(string sLabel, object oObject, string sColor) { NodeLabel = sLabel; NodeTag = oObject; Color = sColor; } public string NodeLabel { get; set; } public object NodeTag { get; set; } public string Color { get; set; } private ObservableCollection<ReportNode> \_ChildNodes; public ObservableCollection<ReportNode> ChildNodes { get { return \_ChildNodes; } set { \_ChildNodes = value; base.OnPropertyChanged("ChildNodes"); } } }
Hierarchical data template binds the itemssource to the ChildNodes collection. When loading the nodes I stuff the NodeLabel with the text to display and the list object goes in the NodeTag property. When accessing the node I test the typeof the nodetag to determine what to do with the node. I have 1 tree with 6 different data sources, works perfectly.
Never underestimate the power of human stupidity RAH