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

Thanks,

Eric Bergman-Terrell

----------------------------------------------------------------------------------------------------------------------------------------------------------------------



<Window x:Class="HierarchicalDataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XML Data Binding" Height="300" Width="300">
<Window.Resources>
<HierarchicalDataTemplate DataType="GameStats" ItemsSource="{Binding XPath=*}">
<TextBlock FontStyle="Italic" Text="All Game Stats" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="GameStat" ItemsSource="{Binding XPath=*}">
<StackPanel>
<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" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="HighScore">
<TextBox Text="{Binding XPath=., Mode=TwoWay}" />
</DataTemplate>
<XmlDataProvider x:Key="dataProvider" XPath="GameStats">
<x:XData>
<GameStats xmlns="">
<GameStat Type="Beginner">
<HighScore>1203</HighScore>
</GameStat>
<GameStat Type="Intermediate">
<HighScore>1089</HighScore>
</GameStat>
<GameStat Type="Advanced">
<HighScore>541</HighScore>
</GameStat>
</GameStats>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<TreeView Name="tv" ItemsSource="{Binding Source={StaticResource dataProvider}, XPath=.}" />
<TextBlock Name="txtblk" Text="hello" />
</StackPanel>
</Window>



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">
<Setter.Value>
<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....

Regards