专栏首页hbbliyongWPF TreeView SelectedItemChanged called twice

WPF TreeView SelectedItemChanged called twice

How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. But SelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up.

What we have to do to avoid this is simply delay the call to our code, i.e., MyFunction() which we need to execute in SelectedItemChanged. Here's a workaround which delays the call to open the new window until the item selection code finishes up:

private delegate void NoArgDelegate();

void Window1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
  Dispatcher.BeginInvoke(DispatcherPriority.Background, 
        (NoArgDelegate)delegate { MyFunction(); });
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 5步搭建GO环境

    Easy Go Programming Setup for Windows Dec 23, 2014 I’ve had to do this more t...

    hbbliyong
  • WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

    1.TreeView选择事件执行两次 Very often, we need to execute some code in SelectedItemChang...

    hbbliyong
  • MySQL 8.0版本连接报错:Could not create connection to database server.

     准备搭建一个Spring Boot 组合mybatis的项目,数据库采用的是MySQL 8.0.11按照以往的配置,使用插件mybatis-generator...

    hbbliyong
  • Semantic Communications for Speech Signals (cs.SD)

    我们考虑了一种语音信号的语义通信系统,命名为SCS。在深度学习的突破下,我们探索语音语义在接收机上恢复语音的语义意义,其目的是使语音语义错误最小化,而不是传统通...

    用户8078797
  • 来自预测市场的及时信息(CS CSGT)

    预测市场是从战略代理人那里引出和聚集信念的有力工具。然而,在当前的预测市场中,代理人可能会通过竞争成为第一个更新市场的人来耗尽社会福利。我们开始研究信息被市场聚...

    识檐
  • 聊聊flink的PrintSinkFunction

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/data...

    codecraft
  • 聊聊flink的PrintSinkFunction

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/data...

    codecraft
  • Go内存模型

    Go语言中内存分配大致有3种模式:Stack、Heap、Fixed Size Segment。

    后台搬砖鹅
  • 【CodeForces 599A】D - 特别水的题4- Patrick and Shopping

    Today Patrick waits for a visit from his friend Spongebob. To prepare for the vi...

    饶文津
  • copy()

    青木

扫码关注云+社区

领取腾讯云代金券