Eric Bergman-Terrell

I'm populating a TreeView using XML Data Binding. By default, the controls that are generated are indented, as one would expect.

How can I selectively suppress the indentation For example, how can I ensure that The "zzz" TextBlock is not indented


Eric Bergman-Terrell


<Window x:Class="HierarchicalDataBinding.Window1"
Title="XML Data Binding" Height="300" Width="300">
<HierarchicalDataTemplate DataType="GameStats" ItemsSource="{Binding XPath=*}">
<TextBlock FontStyle="Italic" Text="All Game Stats" />
<HierarchicalDataTemplate DataType="GameStat" ItemsSource="{Binding XPath=*}">
<TextBlock FontWeight="Bold" FontSize="20" Text="{Binding XPath=@Type}" />
<TextBlock Text="{Binding ElementName=txtblk, Path=Text}" />
<TextBlock Text="{Binding ElementName=txtblk, Path=Margin.Left}" />
<TextBlock Name="zzz" Text="{Binding ElementName=zzz, Path=DataContext}" Initialized="zzz_Initialized" />
<DataTemplate DataType="HighScore">
<TextBox Text="{Binding XPath=., Mode=TwoWay}" />
<XmlDataProvider x:Key="dataProvider" XPath="GameStats">
<GameStats xmlns="">
<GameStat Type="Beginner">
<GameStat Type="Intermediate">
<GameStat Type="Advanced">
<TreeView Name="tv" ItemsSource="{Binding Source={StaticResource dataProvider}, XPath=.}" />
<TextBlock Name="txtblk" Text="hello" />

Re: Windows Presentation Foundation (WPF) Selectively Suppress TreeView Indentation

Marlon Grech

Hi there what you need to do is a ControlTemplate on the TreeViewItem type like the following

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type TreeViewItem}">

put this in the resources section of the TreeView and do what ever formating you need...

The reason why you need to use a control template and not a data template is that the treeviewitem will force whatever control you put in the datatemplate to be indented, on the othere hand when using the control template on the tree view itme you can force whatever style you want for the treeviewitem...

I hope this helps get back to me if you need any more help....