简体   繁体   中英

KeyDown Event Not Firing After Drag Drop Operation

I have a custom editable text box in a template for a TreeViewItem. The items can be edited when F2 key is pressed. If not in edit mode, when the item is clicked, a drag and drop operation is triggered. This is my template for the editable tree view item:

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

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

The EditBox is custom control which simply switches from a "read only" to editable TextBox. In the "EditBox_KeyDown" method I simply set the IsInEditMode property of the EditBox to true, and it does its magic. However, this event is not fired after I handle the "PreviewMouseLeftButtonDown" as follows:

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

I assume that, once the drag drop operation is started, it simply absorbs all key events. How do I cancel this?

The problem was, in fact, the lost focus. Important part of solving the issue was that my EditBox was displaying uneditable TextBox when the items is not being edited. TextBox is a focusable control. I chnaged this to TextBlock, which is not focusable. For drag and drop operations I ended up using the following approach:

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

The combination of these two changes solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM