惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 司徒正美
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
Help Net Security
Help Net Security
N
News and Events Feed by Topic
O
OpenAI News
L
LangChain Blog
F
Full Disclosure
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
Cloudbric
Cloudbric
W
WeLiveSecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
Attack and Defense Labs
Attack and Defense Labs
PCI Perspectives
PCI Perspectives
TaoSecurity Blog
TaoSecurity Blog
AI
AI
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
T
Threatpost
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Last Week in AI
Last Week in AI
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
P
Proofpoint News Feed
Latest news
Latest news
S
SegmentFault 最新的问题
J
Java Code Geeks
T
Threat Research - Cisco Blogs
H
Help Net Security
P
Privacy International News Feed

博客园 - 风中灵药

如何在mvc中共用一个dbconext以节省资源提高性能? asp.net core api路由及多语言切换的实现 mp3转speex的一些研究(貌似不能播放,暂存着) 安装win7 64位和win10 64位双系统小结 macbook装双系统多分区其实很简单,你只要把macbook当作一台普通pc就可以了! 个人对AutoResetEvent和ManualResetEvent的理解 一条语句实现查询各类别前10条记录 【原】Sql2005 实现递归 【转】javascript操作cookies 以及 正确使用cookies的属性 回归: css, bug bug, background-repeat and frames 如何用oledb读取dbf(FoxPro表)文件? SQL注入中利用XP_cmdshell提权的用法(转) Sql 2005自动备份并自动删除3天前的备份 ActiveX开发心得(原创) 常用正则表达式(包括中文匹配) 使用ASP.NET Global.asax 文件(转) 如何不打开文件 直接出现下载保存提示框 Replace ntext类型中的文字 关于IFRAME 自适应高度的研究[转]
关于WPF下ComBox的SelectionChanged事件
风中灵药 · 2012-11-17 · via 博客园 - 风中灵药

使用MVVM模式时,在Loaded事件中设置与ComBox.SelectedItem关联的属性时不会触发ComBox的SelectionChanged事件。

如何使页面加载时触发这一事件呢?可调用外部线程操作, 方法有二,1是使用timer类,2是使用Thread类。伪代码如下:

        

private void PageLoad(object o)
        {
            _thisWindow = o as Window;
            Thread t = new Thread(delegate()
            {
                _thisWindow.Dispatcher.Invoke(new Action(delegate()
                {
                    PageLoad();
                }));
            });
            t.Start();
        }

        private void PageLoad()
        {
           this.SeletcedItem = "ok";
           //do some thing...

        }  

 另外,如果外部线程调用某个类,当这个类触发某事件时,事件处理方法中一定要加上类似的语句: 

            Thread t = new Thread(delegate()
            {
                _thisWindow.Dispatcher.Invoke(new Action(delegate()
                {
                    this.SeletcedItem = "ok";
                }));
            });

 也就是Dispatcher.Invoke()方法,否则ComBox的SelectionChanged事件也无法触发。

 找到这些经验,花了不少代价啊!!!

补充:

我找到出现开关所述问题的原因所在了,如果直接xaml中设置Combox.SelectionChanged="SelectionChanged",那么在Loaded时,SelectionChanged事件会正常触发。

但我用的是Triggers:

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="SelectionChanged">

                    <i:InvokeCommandAction Command="{Binding ProvinceSelectionChangedCommand}"/>

                </i:EventTrigger>

            </i:Interaction.Triggers>

这样在页面加载的时候会没有用。我猜是因为loaded时,Triggers还没生效,也就是说要等页面加载完毕Triggers才会被实例化。

我以前就有这种猜测,果然不出所料啊!这也是mvvm的一个弊端吧!