Note that there are some explanatory texts on larger screens.

plurals
  1. POEditable WPF treeview item on doubleclick? (with styles?)
    primarykey
    data
    text
    <p>I'm a bit of a WPF noob, so apologies for any inherent daftness in this question (!)</p> <p>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.</p> <p>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'.</p> <p>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).</p> <p>Using a full blown custom control seems like the alternative - there's an AAALMOST working example here ( <a href="http://www.codeproject.com/KB/WPF/editabletextblock.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/WPF/editabletextblock.aspx</a> ), however it doesn't work in the presence of HierachicalDataTemplate clauses (and it doesn't look like a solution is forthcoming).</p> <p>(eg 1 - switch from textblock to textbox when selected)</p> <pre><code>&lt;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"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="EditableContentControl" TargetType="{x:Type ContentControl}"&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate DataType="{x:Type local:CompositeViewModel}"&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsSelected,RelativeSource=RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True"&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate DataType="{x:Type local:CompositeViewModel}"&gt; &lt;TextBox Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TreeView Margin="12,12,115,12" Name="treeView1" ItemsSource="{Binding Path=GetRootData}" &gt; &lt;TreeView.ItemTemplate&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:CompositeViewModel}" ItemsSource="{Binding Path=Children}"&gt; &lt;ContentControl Content="{Binding}" Style="{StaticResource EditableContentControl}"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.ItemTemplate&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload