Tutorial :Having trouble with ItemContainerStyle on a Treeview



Question:

I have the following XML being set on my treeview:

<Root Value="YES">   <Child Name="Test">   <Sibling Data="Yes">    <Last UserData="1"/>   </Sibling>   <Sibling Data="No"/>   </Child>   <Child Name="Test2"/>  </Root>  

and then I have set the following code in my window:

<Window.Resources>      <XmlDataProvider x:Key="dataProvider" XPath="Root" Source="C:\XML.xml" />        <HierarchicalDataTemplate DataType="Root" ItemsSource="{Binding XPath=*}">        <TextBlock Text="{Binding XPath=@Value}" />          <HierarchicalDataTemplate.ItemContainerStyle>          <Style TargetType="{x:Type TreeViewItem}">            <Setter Property="Template">              <Setter.Value>                <ControlTemplate>                  <Border Background="Blue">                    <ContentPresenter/>                  </Border>                </ControlTemplate>              </Setter.Value>            </Setter>          </Style>        </HierarchicalDataTemplate.ItemContainerStyle>      </HierarchicalDataTemplate>        <HierarchicalDataTemplate DataType="Child" ItemsSource="{Binding XPath=*}">        <TextBlock Text="{Binding XPath=@Name}" />      </HierarchicalDataTemplate>        <HierarchicalDataTemplate DataType="Sibling" ItemsSource="{Binding XPath=*}">        <TextBlock Text="{Binding XPath=@Data}" />      </HierarchicalDataTemplate>    </Window.Resources>      <Grid>      <TreeView Margin="12" x:Name="trv"           ItemsSource="{Binding Source={StaticResource dataProvider}}" />    </Grid>  

I would like to have a border control wrap around all the subitems for every node as in this image:

http://www.hardcodet.net/uploads/2008/03/tree-dialogik.png

In other words, you'll notice in the image I've linked to, the parent node dialogik.Memory has a dark gray border that goes around it and around its children. That's the effect I want to achieve.

What do I need to change in my code to have it work correctly ???

Thanks !!


Solution:1

That's not possible with the TreeView. You could use an Expander though, see here:

http://msdn.microsoft.com/en-us/library/system.windows.controls.expander.aspx

..and then bind it similarly: you'd have an ItemsControl that outputs an Expander for each item, and the Expander would then contain another ItemsControl for child items, recursively.

Hope that helps!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »