How to implement In-Place Editing on a TreeViewItem?

Has anyone done this? It doesn't look like the framework automatically supports it. How much work is involed? I could just throw a dialog box, but In-Place would be much better.
[184 byte] By [JeremyChaney] at [2008-1-9]
# 1
Simply put a TextBox inside your TreeViewItem and voila, you have in place editing. If you want to be able to toggle between TextBox and TextBlock, you have to make a custom control, or put a grid in the TreeViewItem that contains a TextBox and a TextBlock, only make the TextBox visible when a command is activated.
Bragi at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

You don't actual *need* to make a custom control, although the functionality may better match what you're looking for if you use one. Using Styles/Triggers, you can create the basic functionality:

<Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="TreeViewItem">

<Setter Property="HeaderTemplate">

<Setter.Value>

<DataTemplate>

<TextBlock Text="{Binding}" />

</DataTemplate>

</Setter.Value>

</Setter>

<Style.Triggers>

<Trigger Property="IsSelected" Value="true">

<Setter Property="HeaderTemplate">

<Setter.Value>

<DataTemplate>

<!-- Must use RelativeSource, since we can't TwoWay bind without a Path -->

<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=Header}" />

</DataTemplate>

</Setter.Value>

</Setter>

</Trigger>

</Style.Triggers>

</Style>

Hope this Helps.

T.J. Hsiang

WPF Performance Team

T.J.Hsiang(Microsoft) at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3
Thanks for the suggestion, but I think I will go with the custom control approach. I don't want the items to become editable just because somebody clicked on them. I would rather make the control follow standard rename functionality (F2, Rename Menu Item,...).
JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

Yeah, the control you're talking about (let's say "EditBox") has been a popular feature request and we have looked into adding support for it in the future, although we can't commit doing so at this point.

Good luck,

T.J. Hsiang

WPF Performance Team

T.J.Hsiang(Microsoft) at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

I have already made such a control. If people are interested, I wil put it in to the wpf contrib lib. I think the control could use some modifs though (which is why I haven't released it yet). It uses the grid example I wrote about in my previous post (put the 2 on top of each other, make one visible depending on the state of the object, state is a property). I think the MSDN has an example which creates a new TextBox dynamically. I think this is a little cleaner.

Let me know if anyone is interested in the control, and I'll release it.

Bragi at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6
I would definitely be interested in seeing your control. Also, if you have a link to the MSDN example, I'd like to see that too. I've looked for something like that, but haven't found anything.
JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7

This article shows how to implement in-place editing, using a custom control to host the TextBox while in edit mode: http://www.codeproject.com/WPF/AnnotatingAnImageInWPF.asp

You could probably put that control into a TreeViewItem to accomplish what you're after.

JoshSmith at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8
[I thought I posted this yesterday, but apparently I didn't. This question may be outdated by Josh Smith's post, but I'll ask it anyway 'cause I'd like to know the answer]

OK, I give. I can't figure out how to add a TextBox to my TreeViewItem. I've seen several samples on the web that show how to customize the TreeViewItem style, but I'm confused as to how that relates to what is in my HierarchicalDataTemplate. In the TreeViewItem style there is no mention of a TextBlock, just a ContentPresentor. In my DataTemplate, though, TextBlock is in there twice. I can't however, add a hidden TextBox. Here is my datatemplate, any suggestions?

Code Snippet

<HierarchicalDataTemplate ItemsSource="{Binding Path=Procedures}">

<TextBlock x:Name="textBlock" FontWeight="Bold" Text="{Binding Path=Name}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>


Also, for the Microsoft guys, what happend to the PDC this year? Are there any other conferences that cover this stuff in depth? Reading how all of this stuff works is one thing, but I'd really like hear it explained and be able to ask questions.
Thanks,
--Jeremy

JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 9
It turns out that Josh Smith's post didn't answer my question. It helps some, and demonstrates how to do some cool things (and I stole his picture of Homer and made it my desktop background ), but I still don't know how to customize a TreeViewItem so that I can include the TextBlock and a TextBox while still using a DataTemplate.

JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 10
For the DataTemplate, try:

<HierarchicalDataTemplate DataType="{x:Type i:XXX}"
ItemsSource="{Binding Path=Values}"
>
<StackPanel Orientation="Horizontal">
<Image Source="{DynamicResource ImgInstValueGroup}"
Width="16"
Height="16"
RenderOptions.EdgeMode="Aliased"/>
<Grid MinWidth="200">
<TextBlock Text="{Binding Path=Name}"
Visibility="{Binding ElementName=TxtContent, Path=Visibility, Converter={StaticResource VisInvers}}"
Style="{StaticResource TextBlockStyle}"/>
<TextBox Name="TxtContent"
AcceptsReturn="False"
Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
ContextMenu="{StaticResource ValuesContextMenu}"
Visibility="{Binding ElementName=ParentControl, Path=EditMode, Converter={StaticResource BoolToVis}}"
/>
</Grid>
</StackPanel>
</HierarchicalDataTemplate>

This technique uses a custom converter, which simply reverses the 'Visibility' value (Visible <-> Collapsed). And I have a property defined on the page called 'EditMode' which, when true, all the items using this template will be put in edit mode.

This is one way of doing it, without using custom controls. There is some overhead involved with this type of solution (there is this extra grid and the 2 controls are always created). I will post a control to http://www.codeplex.com/wpfcontrib in a couple of days, which will be a more generic solution. I am probably going the to inherit from a 'Decorator'. This way, you can add In place editing behaviour to all types of controls, not just textblocks, using all kinds of editors (ex ComboBoxes). Keep an eye on the project, I will post it in a couple of days, maybe a week (note: the next actual release will be in a couple of weeks, so you will have to get the latest sources).

Bragi at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 11

I think this post has the answer that you need

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2096191&SiteID=1

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 12
Thank you Marlon- that post got me almost all the way there. There is one more problem that I'm having now, though. I'm having trouble getting mouse events in the TextBox. I get the doubleclick event, but not leftbuttondown. Any ideas? Here is my XAML:

Code Snippet

<TreeView.ItemTemplate>

<HierarchicalDataTemplate ItemsSource="{Binding Path=Procedures}">

<TextBox Text="{Binding Path=Name}" Background="Transparent" BorderBrush="Transparent" x:Name="node" IsReadOnly="True" MouseLeftButtonDown="CategoryBrowserTreeViewItem_MouseLeftButtonDown" MouseDoubleClick="CategoryBrowserTree_DoubleClickEvent" KeyDown="CategoryBrowserTree_KeyDown" />

</HierarchicalDataTemplate>

</TreeView.ItemTemplate>

JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 13

use can use the mousedown and check which button as been clicked from the eventargs... i am sorry but i don't know why the event is not firing
MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 14
MouseDown doesn't get called either. I'm reading an MSDN article about "Working Around Event Suppression by Controls". Hopefully that will shed some light on the topic.
JeremyChaney at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified