简体   繁体   中英

How to trigger TreeView LostFocus event

I have a TreeView like this:

        <TreeView LostFocus="treeView_LostFocus">
            <TreeViewItem Selected="treeViewItem_select"/>
        </TreeView>

it always trigger event treeView_LostFocus first when I select different item in TreeViewItem using treeViewItem_select . How can I do to trigger the lost focus event only when the the TreeView losing focus, not before trigger treeViewItem_select ?

ps: make e.Handle = true in the end of treeViewItem_select seems not work.

A solution is to check if the KeyboardFocus is still within the TreeView.

private void treeView_LostFocus(object sender, RoutedEventArgs e)
{
    var tv = sender as TreeView;

    if (tv.IsKeyboardFocusWithin == false)
    {
        //Your code
    }
}

That way when your child items get focus, your code won't get executed. Only when the whole TreeView loses focus.

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