Reputation: 1011
I cant seem to get the Header to bind to my style for this treeview i have created Ive kinda styled the crap out of this control lol.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="12*" MaxWidth="400"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<TreeView Name="WizardMenu" Visibility="Collapsed">
<TreeViewItem Name="treeViewItem" IsExpanded="True"
Header="Hardware Information"
HeaderTemplate="{StaticResource WizardsMenuItem}"
>
</TreeViewItem>
<TreeViewItem Name="treeViewItem1" IsExpanded="True"
Header="Hardware Information"
HeaderTemplate="{StaticResource WizardsMenuItem}"
>
</TreeViewItem>
<TreeViewItem Name="treeViewItem2" IsExpanded="True"
Header="Hardware Information"
HeaderTemplate="{StaticResource WizardsMenuItem}"
>
</TreeViewItem>
</TreeView>
Code that created the nodes
Code that styles the nodes
<HierarchicalDataTemplate x:Key="WizardsMenuItem" >
<TreeViewItem ClipToBounds="True">
<TreeViewItem.HeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}">
<CheckBox.Template>
<ControlTemplate>
<DockPanel MaxWidth="365">
<Border Margin="5" Height="80" BorderBrush="Black" BorderThickness="1" CornerRadius="5" >
<DockPanel>
<DockPanel>
<DockPanel Name="IconArea" DockPanel.Dock="Right">
<Border Margin="4" Width="80" BorderBrush="Black" BorderThickness="1" CornerRadius="5" DockPanel.Dock="Right">
<Image />
</Border>
</DockPanel>
<DockPanel Name="TextArea" DockPanel.Dock="Left">
<TextBlock Text="{Binding}" Foreground="Black" DockPanel.Dock="Top" Margin="3,10,3,10" FontSize="18" FontWeight="Bold" FontFamily="Tahoma" />
<TextBlock Text="Basic Hardware, Disk Drive Space, Memory Size, Memory Allocation"
DockPanel.Dock="Top"
Margin="13,0,0,0"
FontFamily="Verdana" FontSize="9"
TextWrapping="Wrap" />
</DockPanel>
</DockPanel>
</DockPanel>
</Border>
</DockPanel>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
</DataTemplate>
</TreeViewItem.HeaderTemplate>
<TreeViewItem>
<TreeViewItem.HeaderTemplate>
<DataTemplate>
<Border Margin="30,0,5,0" BorderBrush="Black" BorderThickness="1" CornerRadius="5" >
<DockPanel>
<Button DockPanel.Dock="Top" Margin="5,2,5,2" Content="Predefined Reports" Background="Transparent" BorderBrush="Transparent" />
<Button DockPanel.Dock="Top" Margin="5,2,5,2" Content="Favorites Reports" Background="Transparent" BorderBrush="Transparent" />
<Button DockPanel.Dock="Top" Margin="5,2,5,2" Content="Customize Reports" Background="Transparent" BorderBrush="Transparent" />
</DockPanel>
</Border>
</DataTemplate>
</TreeViewItem.HeaderTemplate>
</TreeViewItem>
</TreeViewItem>
</HierarchicalDataTemplate>
Everything is working except for this bit
<TextBlock Text="{Binding}" Foreground="Black" DockPanel.Dock="Top" Margin="3,10,3,10"
FontSize="18" FontWeight="Bold" FontFamily="Tahoma" />
I hope that someone could please help me
Thank you in advance :)
Upvotes: 1
Views: 126
Reputation: 1624
The problem is your 'Textbox' has no DataContext, so that '{Binding}' - returns nothing
I guess that you want to bind it to the header attribute in TreeViewItem (Hardware Information)
your easiest option is to bind the textbox to it's relative anchestor - TreeViewItem's header
{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Header}
but that will bind to the nearest anchestor, which is in your Template (inside the HierarchicalDataTemplate you have another TreeViewItem) Then you must also bind his 'Header' to it's parent the same way
<TreeViewItem ClipToBounds="True" Header="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Header}">
That will work
BUT - the easiest way is not necessary the best here (and probably not) I didn't understand why you used HeaderTemplate and HierarchicalDataTemplate. I think you should just replace the TreeViewItem.Template in a ControlTemplate where you will replace the whole TreeViewItem and not just it's header. But maybe you have another plan in mind so the first solution will also work.
Upvotes: 1