双击时可编辑的 WPF 树视图项?(有风格?) [英] Editable WPF treeview item on doubleclick? (with styles?)
问题描述
我有点像 WPF 菜鸟,因此对于这个问题中的任何内在愚蠢,我深表歉意 (!)
I'm a bit of a WPF noob, so apologies for any inherent daftness in this question (!)
我正在尝试通过双击启用对 WPF 树视图标签的编辑 - 我已经搜索了这个,看起来这样做的两种方法是使用自定义控件或隐藏文本框之一的样式/TextBlock.
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.
使用样式将标签设置为基于 DataTrigger 的文本框似乎很容易(例如下面的 1),但这意味着任何时候选择一行,它都是正在编辑".
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'.
我真正想做的是在 mousedoubleclick 事件上启用此功能(转换到文本框),但似乎 EventTriggers 不能以下面的方式使用,因为它们是瞬态的.(似乎我不能简单地在代码隐藏中使用 DoubleClick 事件,因为这不会 (??) 允许我影响显示的控件以显示/隐藏文本框).
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).
使用完整的自定义控件似乎是另一种选择 - 这里有一个 AAALMOST 工作示例(http://www.codeproject.com/KB/WPF/editabletextblock.aspx ),但是它在 HierachicalDataTemplate 子句存在的情况下不起作用(并且看起来似乎没有解决方案).
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).
(例如 1 - 选中时从文本块切换到文本框)
(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>
推荐答案
是否有帮助:
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.
这篇关于双击时可编辑的 WPF 树视图项?(有风格?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!