I am a little WPF noob, so I apologize for any inherent inaccuracy in this matter (!)
I am trying to enable WPF tree shortcut editing using doubleclick - I was looking for this problem, and it looks like the two ways to do this are a custom control or a style that hides one of the TextBox / TextBlock.
Using a style to set a textBox shortcut based on a DataTrigger seems simple enough (for example, 1 below), but that means that at any time when a row is selected, it is โeditedโ.
I would really like to do this (go to the text box) on the mousedoubleclick event, but it seems that EventTriggers cannot be used as follows, because they are temporary. (It seems that I cannot just use the DoubleClick event in codebehind, because it does not allow (??) to allow me to influence the displayed controls in order to show / hide text fields).
Using a full-scale custom control is similar to an alternative - there is a working example AAALMOST ( http://www.codeproject.com/KB/WPF/editabletextblock.aspx ), however it doesnโt work if there are HierachicalDataTemplate suggestions (and this does not seem to what the decision received).
(for example, 1 - switch from a text block to a text field, if selected)
<Window x:Class="treetest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:treetest" Title="Window1" Height="300" Width="300"> <Window.Resources> <Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate DataType="{x:Type local:CompositeViewModel}"> <TextBlock Text="{Binding Path=Name}" /> </DataTemplate> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource=RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate DataType="{x:Type local:CompositeViewModel}"> <TextBox Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <TreeView Margin="12,12,115,12" Name="treeView1" ItemsSource="{Binding Path=GetRootData}" > <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type local:CompositeViewModel}" ItemsSource="{Binding Path=Children}"> <ContentControl Content="{Binding}" Style="{StaticResource EditableContentControl}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Grid> </Window>
lab27 source share