Editable TreeView tree item for WPF? (with styles?)

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> 
+4
source share
3 answers

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 is quite simple and works fine for me.

+4
source

What if, instead of running on IsSelected, you run related data, such as IsEditing, on a custom property? You can then set IsEditing to true when you want to change (for example, in your case, when the mouse button is clicked).

0
source

Check out CallActionMethod from Blend. This special trigger allows you to freely communicate between any event, such as double-clicking and a method in your code.

If you prefer to use the command, you can do the same with InvokeCommandAction . You can connect any command to the event.

0
source

Source: https://habr.com/ru/post/1308056/


All Articles