WPF:为什么在创建图表控件之前就触发了Combobox SelectionChanged事件 [英] WPF: why my Combobox SelectionChanged event is fired before my chart control created

查看:126
本文介绍了WPF:为什么在创建图表控件之前就触发了Combobox SelectionChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有应用程序和另一个子窗体:

So i have application and another sub form:

public partial class SubForm: MetroWindow...

这是我从主菜单中打开子表单的方式 form

And here is how i am open my Sub form from my main form:

SubForm subForm = new SubForm();
subForm.ShowDialog();

我的子表单里面有这个图表控件:

<telerik:RadCartesianChart
    x:Name="chart" />

Combobox

<ComboBox
    Name="cbInterfaces" 
    ItemsSource="{Binding Path=(my:MyClass.MachineInterfaces)}"
    SelectedIndex="0"
    SelectionChanged="cbInterfaces_SelectionChanged"/>

所以我注意到在子表单之后在 InitializeComponent 方法之后操作,代码进入了我的 Combobox SelectionChanged 事件和我的图表控件仍为 null 且尚未创建。
因此,直到再次使用我的组合框并再次更改选择(在这种情况下,我的图表不为空)

So i notice that after the Sub form oped right after InitializeComponent method, the code is go into my Combobox SelectionChanged event and my chart control is still null and not created yet. So i cannot use it until use again my Combobox and change selection again (in this case my chart not null)

推荐答案

如果窗口或 RadCartesianChart 尚未初始化或加载:

You could just return from the event handler immediately if the window or the RadCartesianChart haven't yet been initialized or loaded:

private void cbInterfaces_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (!this.IsLoaded || chart == null || !chart.IsLoaded)
        return; //do nothing

    //your code...
}




是的,但是问题在于,创建并打开此表单后,我想立即在聊天中看到鼻烟,而不是再次更改组合框选择...

Yes but the problem is that after this form created and opened i want to see my snuff immediately in my chat instead of change my combobox selection again ...

在调用 InitializeComponent() SelectedIndex 属性c $ c>方法,然后:

Set the SelectedIndex property programmatically after the call to the InitializeComponent() method then:

public partial class SubForm : Window
{
    public SubForm()
    {
        InitializeComponent();
        cbInterfaces.SelectedIndex = 0;
    }

    private void cbInterfaces_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //...
    }
}







<ComboBox
    Name="cbInterfaces" 
    ItemsSource="{Binding Path=(local:MyClass.MachineInterfaces)}"
    SelectionChanged="cbInterfaces_SelectionChanged"/>

这篇关于WPF:为什么在创建图表控件之前就触发了Combobox SelectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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