Project Description
This project is based on Josh Smith's great article Simplifying the WPF TreeView by Using the ViewMode on codeproject.
In a WPF project it's often a requirement to implement in-place editing in the TreeView control, however, doing it in a incorrect way could make the whole thing complicated.
This project demonstrates a clean way of doing the in-place editing in TreeView - The logic of editing is encapsulated in attached behaviors, it's very easy to apply to any TreeView and requires NO change to the view model.
1. Press F2 or click the item twice (not double click) to begin editing the selected item
2. Press Esc or click the white space or another item to cancel editing
3. Press Enter to confirm editing

What you really need to do is just setting up the TreeView like this and run.

<TreeView ItemsSource="{Binding FirstGeneration}" Behaviors:TreeViewInPlaceEditBehavior.IsEditable="True">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>

<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Grid>
<TextBlock Text="{Binding Name}" />
<TextBox Text="{Binding Name, UpdateSourceTrigger=Explicit}" Visibility="Collapsed" x:Name="TextBox"/>
</Grid>
<HierarchicalDataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected}" Value="True"/>
<Condition Binding="{Binding Path=(Behaviors:TreeViewInPlaceEditBehavior.IsEditing), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="TextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditing">
<Setter.Value>
<Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditing)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
</Setter.Value>
</Setter>
<Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditConfirmed">
<Setter.Value>
<Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditConfirmed)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
</Setter.Value>
</Setter>
<Setter TargetName="TextBox" Property="Behaviors:TreeViewInPlaceEditBoxBehavior.IsEditCancelled">
<Setter.Value>
<Binding Path="(Behaviors:TreeViewInPlaceEditBehavior.IsEditCancelled)" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}"/>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

Last edited Sep 24, 2012 at 4:22 PM by eagleboost, version 7