WPF:为什么在创建图表控件之前就触发了Combobox SelectionChanged事件 [英] WPF: why my Combobox SelectionChanged event is fired before my chart control created
问题描述
所以我有应用程序和另一个子窗体:
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屋!