开发者

wpf combo change event triggered when form loads

开发者 https://www.devze.com 2023-02-07 09:47 出处:网络
I am having a problem, when window loads, the \'selection_change\' event associated with \'combo box\' control is getting triggered when the window loads first time. Why its occuring开发者_如何学Pytho

I am having a problem, when window loads, the 'selection_change' event associated with 'combo box' control is getting triggered when the window loads first time. Why its occuring开发者_如何学Python and How to restrict it please?

Regards


With that code, the SelectionChanged event won't get raised. Create a new project, paste it and try it for yourself.

My guess is pretty much the same as Sekhar_ Pro's, you're populating your ComboBox from code behind, and something in there causes the SelectedItem to change. Investigate the cmbUsers.SelectedItem in the cmbUsers_SelectionChanged event handler to see if it has some value or is null in the debugger. Also, look in the Call Stack to find what caused this event to be raised.

Example code

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        cmbUsers.Items.Add(new ComboBoxItem { Content = "Test" });
        cmbUsers.SelectedIndex = 0;
    }
    private void cmbUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (cmbUsers.SelectedItem != null)
        {
            MessageBox.Show(cmbUsers.SelectedItem.ToString());
        }
    }
}

The Call Stack looks like this for me in the event handler

wpf combo change event triggered when form loads


This is not a normal Behavior, some where you must be doing something like setting SelectedItem, etc which in turn is triggering the event. Check thorough your form's life-cycle events and see if you are doing something like this, may be in Load or Activate event or somewhere in the Constructors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号