XAML ComboBox SelectionChanged触发OnLoad [英] XAML ComboBox SelectionChanged Fires OnLoad

查看:93
本文介绍了XAML ComboBox SelectionChanged触发OnLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个具有SelectionChanged事件的ComboBox,则在加载控件时会触发它.

If I have a ComboBox that has a SelectionChanged event, it fires when I'm loading the control.

因此,在页面加载时,我设置了SelectedValue,并且触发了SelectionChanged事件,这不是我想要的.

So at page load I set the SelectedValue and the SelectionChanged event fires which is not what I want to happen.

阻止这种行为的公认方法是什么?

What is the accepted apporach to stopping this?

推荐答案

两个明显的解决方案是1)等待直到包含ComboBox的Window/Page/UserControl的Loaded事件并在那里连接SelectionChanged ...例如在构造函数中:

Two obvious solutions to this would be 1) Wait until the Loaded event of the Window/Page/UserControl which contains the ComboBox and hook up SelectionChanged there...eg in the constructor:

// set the inital selected index for the combo box here...

this.Loaded += (s, args) =>
               {
                    cmbBox.SelectionChanged += 
                            new SelectionChangedEventHandler(HandleChanged);
               };

或2)在执行任何操作之前,检查ComboBox是否已加载选择更改的处理程序,如果没有,则返回ComboBox,例如:在处理程序中:

or 2) Check that the ComboBox has loaded in the selection changed handler before doing anything and return if it hasn't...eg in the handler:

if (!cmbBox.IsLoaded)
        return;

我希望使用数字1,因为它不需要在每次触发SelectionChanged处理程序时都进行检查.

I would prefer number 1 as it doesn't require the check every time the SelectionChanged handler is fired.

这篇关于XAML ComboBox SelectionChanged触发OnLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆