|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article discusses how to customize the item layout in a WPF This article is not for WPF beginners. It assumes that you already have knowledge of XAML, control templates, styles, triggers, hierarchical data templates, data binding, and other fundamentals of WPF. I also posted another article regarding layout customization for the Graphical overviewBefore diving into the XAML which makes the magic happen, let's first take a look at what we are aiming to achieve. If I populate a
What you see above is certainly not a breathtaking representation of the data. However, after we customize the way that
How it worksThe first step is to create a custom In addition to customizing the Let's take a look at an abridged version of the typed <Style TargetType="TreeViewItem">
<Style.Resources>
<!-- Resources omitted for clarity… -->
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid Margin="2">
<Grid.RowDefinitions>
<!--The top row contains the item's content.-->
<RowDefinition Height="Auto" />
<!--The bottom row contains the item's children.-->
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- This Border and ContentPresenter displays the
content of the TreeViewItem. -->
<Border Name="Bd"
Background="{StaticResource ItemAreaBrush}"
BorderBrush="{StaticResource ItemBorderBrush}"
BorderThickness="0.6"
CornerRadius="8"
Padding="6"
>
<ContentPresenter Name="PART_Header"
ContentSource="Header"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<!-- The ItemsPresenter displays the item's children. -->
<ItemsPresenter Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<!--When the item is selected in the TreeView, use the
"selected" colors and give it a drop shadow. -->
<Trigger Property="IsSelected" Value="True">
<Setter
TargetName="Bd"
Property="Panel.Background"
Value="{StaticResource SelectedItemAreaBrush}" />
<Setter
TargetName="Bd"
Property="Border.BorderBrush"
Value="{StaticResource SelectedItemBorderBrush}" />
<Setter
TargetName="Bd"
Property="TextElement.Foreground"
Value="{DynamicResource
{x:Static SystemColors.HighlightTextBrushKey}}" />
<Setter
TargetName="Bd"
Property="Border.BitmapEffect"
Value="{StaticResource DropShadowEffect}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- Make each TreeViewItem show it's children
in a horizontal StackPanel. -->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel
HorizontalAlignment="Center"
IsItemsHost="True"
Margin="4,6"
Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
The final step is to make the <Window x:Class="CustomTreeViewLayout.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomTreeViewLayout"
Title="Custom TreeView" Height="350" Width="780"
Loaded="OnLoaded"
WindowStartupLocation="CenterScreen"
FontSize="11"
>
<TreeView Name="tree">
<TreeView.Resources>
<ResourceDictionary>
<!-- Import the resource dictionary file which
contains the Style that makes TreeViewItems
display their child items in an organization
chart layout. -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="OrgChartTreeViewItemStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- This template explains how to render
a Node object and its child nodes. -->
<HierarchicalDataTemplate
DataType="{x:Type local:Node}"
ItemsSource="{Binding ChildNodes}"
>
<TextBlock Text="{Binding Text}" />
</HierarchicalDataTemplate>
</ResourceDictionary>
</TreeView.Resources>
<!-- Put the root item(s) in a centered Grid so that
they will be centered and retain their width. -->
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<Grid
HorizontalAlignment="Center"
IsItemsHost="True" />
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
</TreeView>
</Window>
I am not going to discuss the code which populates the TipCustomizing the The Big BummerUnfortunately there is no supported way to programmatically set the selected item in a | ||||||||||||||||||||