拖放操作后KeyDown事件不会触发

时间:2015-04-20 15:43:15

标签: c# wpf xaml

我在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是自定义控件,只需从&#34;只读&#34;可编辑的TextBox。在&#34; EditBox_KeyDown&#34;方法我只是将EditBox的IsInEditMode属性设置为true,它就是它的魔力。但是,此事件未被触发 在我处理&#34; PreviewMouseLeftButtonDown&#34;如下:

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

我认为,一旦拖放操作开始,它就会吸收所有键 事件。我该如何取消?

1 个答案:

答案 0 :(得分:0)

事实上,问题在于失去了焦点。解决这个问题的重要部分是我的EditBox在没有编辑项目时显示不可编辑的TextBox。 TextBox是一个可聚焦的控件。我把它打造成TextBlock,这是无法集中精力的。 对于拖放操作,我最终使用以下方法:

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

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

相关问题