I'm a bit of a WPF noob, so apologies for any inherent daftness in this question (!)
I'm trying to enable editing of WPF treeview labels with a doubleclick - I've googled around this, and it looks like the two ways of doing this are with a custom control or with a style which hides one of a TextBox/TextBlock.
Using a style to set the label to be a textBox based on a DataTrigger seems easy enough (eg 1 below), but it means that any time a row is selected, it's 'being edited'.
What I'd really like to do is to enable this (Transition to textbox) on a mousedoubleclick event, but it seems that EventTriggers can't be used in the manner below, because they're transient. (It doesn't seem I can simply use the DoubleClick event in codebehind, because that doesn't (??) allow me to affect the displayed controls to show / hide the textboxes).
Using a full blown custom control seems like the alternative - there's an AAALMOST working example here ( http://www.codeproject.com/KB/WPF/editabletextblock.aspx ), however it doesn't work in the presence of HierachicalDataTemplate clauses (and it doesn't look like a solution is forthcoming).
(eg 1 - switch from textblock to textbox when 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>
does it help:
string name = "some name";
var treeItem = new TreeViewItem()
{
Header = name,
};
var textBox = new TextBox()
{
Text = name,
};
treeItem.MouseDoubleClick += (o, e) =>
{
TreeItem.Header = textBox;
};
textBox.LostFocus += (o, e) =>
{
treeItem.Header = textBox.Text;
name = textBox.Text;
};
it quite simple and it works for me fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With