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
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 the FontWeight
to Bold
by default, and then there's a DataTrigger
that sets it to Normal
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 the DataTrigger
can check, with a value that tells you whether or not it's a child; just plug that property and value into the style.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider x:Key="Data">
<x:XData>
<Data xmlns="">
<Parent Text="This is a parent">
<Child Text="This is a child"/>
<Child Text="This is a child">
<Grandchild Text="This is a grandchild"/>
<Grandchild Text="This is a grandchild"/>
</Child>
<Child Text="This is a child"/>
</Parent>
<Parent Text="This is a parent">
<Child Text="This is a child"/>
<Child Text="This is a child"/>
<Child Text="This is a child"/>
</Parent>
</Data>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<DockPanel>
<TreeView DockPanel.Dock="Top" ItemsSource="{Binding Source={StaticResource Data}, XPath=/Data/*}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding XPath=*}">
<TextBlock Text="{Binding XPath=@Text}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"/>
<Style.Triggers>
<DataTrigger Binding="{Binding XPath=*}" Value="{x:Null}">
<Setter Property="FontWeight" Value="Normal"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</DockPanel>
</Page>
You need to put something like this:
tb.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
or
tb.SetValue(TextBlock.FontWeightProperty, FontWeights.Normal);
wher you see fit
Try this
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
--------------
--------------
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="true">
<Setter TargetName="PART_Header"
Property=" TextElement.FontWeight"
Value="Bold"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
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