双击可编辑的WPF树形视图项?(带样式?)

7

我对WPF不太熟悉,所以如果这个问题有什么愚蠢的地方请原谅!

我想通过双击启用WPF TreeView标签的编辑 - 我在Google上搜索了一下,看起来有两种方法可以做到:使用自定义控件或使用一个样式来隐藏TextBox/TextBlock中的一个。

使用样式根据DataTrigger设置标签为TextBox似乎很容易(例如下面的1),但这意味着每次选择一行时,它都会处于“正在编辑”状态。

我真正想做的是在鼠标双击事件上启用这个(转换为文本框),但似乎不能像下面这样使用EventTriggers,因为它们是瞬态的。 (似乎我无法简单地在codebehind中使用DoubleClick事件,因为它不能(??)允许我影响显示的控件来显示/隐藏文本框)。

使用完整的自定义控件似乎是另一种选择 - 这里有一个几乎能工作的示例(http://www.codeproject.com/KB/WPF/editabletextblock.aspx),但是它在HierarchicalDataTemplate子句存在的情况下不起作用(似乎也不会有解决方案)。

(例如1 - 当选中时从TextBlock切换到TextBox)

<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>
3个回答

5

它有帮助吗:

        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;
            };

这很简单,对我而言也很有效。


0
如果您改为在绑定数据的自定义属性(例如IsEditing)上触发而不是在IsSelected上触发,会怎样?然后,您可以在希望进行更改时(例如,在鼠标按钮单击时),将IsEditing设置为true。

-1

从Blend中查看CallActionMethod。这个特殊的触发器允许您在任何事件(如双击)和代码后台中的方法之间建立松散的链接。

如果您更喜欢使用命令,则可以使用InvokeCommandAction完成相同的操作。您可以将任何命令连接到事件上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接