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

推荐订阅源

博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
B
Blog
博客园_首页
量子位
博客园 - 叶小钗
L
LangChain Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
D
DataBreaches.Net
雷峰网
雷峰网
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
I
Intezer
H
Help Net Security
The Hacker News
The Hacker News
The Register - Security
The Register - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
Troy Hunt's Blog
L
LINUX DO - 热门话题

博客园 - 风中灵药

如何在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的一个弊端吧!