InitializeComponent() calling another method

后端 未结 2 1872
攒了一身酷
攒了一身酷 2021-01-21 15:54

When I added the SelectionChanged event and of course the method in the code to handle the event to the following combobox:

....



        
相关标签:
2条回答
  • 2021-01-21 16:44

    Put OrderBox.SelectionChanged+=OrderBox_OnSelectionChanged; in the MainWindow constructor and remove the SelectionChanged attribute in your xaml.

    0 讨论(0)
  • 2021-01-21 16:48

    When a form is loaded using the InitializeComponent();, the combo boxes on that pages with be set to (usually) their item at the index of -1, unless you explicitly set it otherwise. Either way, the selected item changes to its default when the form is initialized and the SelectionChanged event hits. You could work around the event being hit by putting a line of error handling:

    private void OrderBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (OrderBox.SelectedItem != null)
        {
            //your code
        }
    }
    
    0 讨论(0)
提交回复
热议问题