I have made a tree view and added parent and child nodes to it all using code behind, now where I am stuck is to set the font of parent nodes to BOLD
while keeping the font of child as it is, below is code that i have right now.
List<ParentTreeViewNode> ParentTreeViewNodeList = new List<ParentTreeViewNode>();
HierarchicalDataTemplate treeViewTemplate = new HierarchicalDataTemplate(typeof(Child));
treeViewTemplate.DataType = "{x:Type local:Child}";
treeViewTemplate.ItemsSource = new Binding("Children");
FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));
tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));
tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow);
treeViewTemplate.VisualTree = tb;
DataTemplate parentTemplate = new DataTemplate(typeof(ParentTreeViewNode));
parentTemplate.DataType = "{x:Type local:ParentTreeViewNode}";
//parentTemplate.ItemsSource = new Binding("Children");
FrameworkElementFactory tbp = new FrameworkElementFactory(typeof(TextBlock));
tbp.SetBinding(TextBlock.TextProperty, new Binding("Name"));
tbp.SetValue(TextBlock.ForegroundProperty, Brushes.Green);
treeViewTemplate.VisualTree = tbp;
// ParentTreeViewNode1.Children = Childlist1;
ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren1"));
ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren2"));
ParentTreeViewNodeList.Add(new ParentTreeViewNode("Paren3"));
//arrayTreeView.ItemTemplate = treeViewTemplate;
arrayTreeView.Resources.Add(1,treeViewTemplate);
arrayTreeView.Resources.Add(2,treeViewTemplate);
arrayTreeView.ItemsSource = ParentTreeViewNodeList;
this is the link where i took help from: http://zamjad.wordpress.com/2009/12/06/using-hierarchical-data-template-with-c-code/#comment-446
thanks
Try this
Copy the default template and add this trigger.I am not sure how to do this in code.You can check this article to get some overview
http://www.codeproject.com/KB/buttons/ButtonControl.aspx
I can't tell you how to fix that code-behind to do that. What I can do is tell you that you don't need code-behind to do this. That looks like a nightmare.
Here's a simple example of how to use binding, a style, and a trigger to accomplish what you're talking about. Don't get too overwhelmed by the fact that I'm using an
XmlDataProvider
here - that's just so that it's a working example that you can paste into Kaxaml and play with. When I say "simple," what I mean is that the whole thing is accomplished by a single binding, a single template, and a single style.The key here is that the style in the
HierarchicalDataTemplate
sets theFontWeight
toBold
by default, and then there's aDataTrigger
that sets it toNormal
if the item has no children. (So you'll notice, as you expand the tree, that the fact that an item is boldfaced tells you that it has children. Which looks kinda nice.)If you're binding to something other than an
XmlDataSource
, you probably have a property on the source that theDataTrigger
can check, with a value that tells you whether or not it's a child; just plug that property and value into the style.You need to put something like this:
or
wher you see fit