【问题标题】:KeyDown Event Not Firing After Drag Drop Operation拖放操作后未触发 KeyDown 事件
【发布时间】:2015-04-20 15:43:15
【问题描述】:

我在 TreeViewItem 的模板中有一个自定义的可编辑文本框。这几项 可以在按下 F2 键时进行编辑。如果不在编辑模式下,当项目是 单击,触发拖放操作。这是我的模板 可编辑的树视图项:

        <HierarchicalDataTemplate x:Key="editableHierarchyNodeTemplate"
                              ItemsSource="{Binding ContainedParameter}"
                              ItemTemplateSelector="{StaticResource treeItemTemplateSelector}">

        <ut:EditBox Text="{Binding DisplayName, Mode=TwoWay}" PreviewMouseLeftButtonDown="EditBox_PreviewMouseDown" PreviewKeyDown="EditBox_KeyDown"/>
    </HierarchicalDataTemplate>

EditBox 是自定义控件,它只是从“只读”切换到可编辑的文本框。在“EditBox_KeyDown”方法中,我只需将 EditBox 的 IsInEditMode 属性设置为 true,它就会发挥作用。但是,此事件不会被触发 在我按如下方式处理“PreviewMouseLeftButtonDown”之后:

  private void EditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        DataObject data = (new DataObject());
        data.SetData("Parameter", (sender as FrameworkElement).DataContext as Parameter);
        DragDrop.DoDragDrop(this, data, DragDropEffects.All);
    }

我假设,一旦开始拖放操作,它就会简单地吸收所有键 事件。如何取消?

【问题讨论】:

  • 已处理 = 假?试过了吗?
  • 可能是焦点问题。当您左键单击控件时,您通常会将焦点设置在控件上,然后它会获取关键事件。但这不再发生,您通过启动 D+D 来吞下左键。解决方法是仅在鼠标移动足够远后才调用 DoDragDrop()。记录 MouseDown 位置并使用 MouseMove 检测它在左键按下时移动。
  • 这主意不错,但是不管从哪里开始拖拽操作,总不会把焦点吞掉吧?

标签: c# wpf xaml


【解决方案1】:

事实上,问题在于失去焦点。解决问题的重要部分是当项目未被编辑时,我的 EditBox 显示不可编辑的 TextBox。 TextBox 是一个可聚焦的控件。我将其更改为不可聚焦的 TextBlock。 对于拖放操作,我最终使用了以下方法:

http://www.wpftutorial.net/DragAndDrop.html

这两个变化的结合解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2021-02-05
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多