c#-选择组合框时出现空引用异常

让我解释一下我的情况.我有wpf用户控件,可以动态生成组合框.为此,我使用类Control Service,从中创建带有组合框项目的组合框,并管理每个组合框的选择更改事件.
在我尝试在第一个组合框上举起第二个事件之前,一切工作都很好.当我尝试清除第二个组合框的项目时出现问题.我做了debubugg,我已经检查过的组合框引用没有问题(childCmB!= null),但是在尝试清除此组合框的项目之后.引发异常.
我尝试在论坛上找到许多案例,但都取得了成功,因此,我希望能提供任何解决此问题的建议.

谢谢

ControlService.class:

 static class ControlService
{
    private static ObservableDictionary<String, System.Windows.UIElement> controls = new ObservableDictionary<String, System.Windows.UIElement>();
    private static Grid expanderContent;

    public static Expander createExpander(String header, String name, String code)
    {
        header = manageControlName(header);
        ExpanderModelView expanderModelView = new ExpanderModelView(name);

        //Recieve template grid
        expanderContent = expanderModelView.getExpanderContent();
        //Add in static collection
        Expander expander = new Expander() { Header = header };
        //Code of provider service
        expander.Uid = code;
        expander.Name = name;

        //Set up handler
        expanderContent.AddHandler(System.Windows.Controls.ComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(cmb_SelectionChanged));
        //Insert data
        expander.Content = expanderContent;

        controls.Add(header, expander);
        return expander;
    }

    public static void cleanUIElementCollection()
    {
        controls.Clear();
    }




    private static string manageControlName(string name)
    {
        int counter = 1;
        if (controls.Count != 0)
        {
            foreach (String value in controls.Keys)
            {
                if (value.Equals(name))
                {
                    if (name.Any(char.IsDigit))
                        name = name.Substring(0, name.IndexOf("("));

                    name = String.Format(name + "({0})", counter);
                    counter++;
                }
            }
        }
        return name;
    }

    private static void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {
        Grid currentContent = sender as Grid;
        String selectedItemId = ((e.Source as System.Windows.Controls.ComboBox).SelectedItem as ComboBoxItem).Uid;
        IEnumerable<Parameter> childCmBs = DataService.getServiceParametersByParentId((e.Source as System.Windows.Controls.ComboBox).Uid);

        if (childCmBs.Any())
        {
            foreach (Parameter param in childCmBs)
            {
                try
                {

                System.Windows.Controls.ComboBox childCmB = currentContent.FindName(param.Key) as System.Windows.Controls.ComboBox;
                if (childCmB != null)
                {
                    childCmB.Items.Clear();
                    addComboBoxItems(DataService.getChildrenParameterValuesByParentId(selectedItemId), childCmB);
                }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(String.Format("Happen some problem during creating comboboxes: {0}", ex.Message));
                }
            }
        }
    }

    public static void createComboBoxes(IEnumerable<Parameter> parameters, System.Windows.Controls.Panel panel)
    {
        foreach (Parameter parameter in parameters)
        {
            TextBlock cmbLabel = new TextBlock() { Text = parameter.Caption };
            System.Windows.Controls.ComboBox cmb = new System.Windows.Controls.ComboBox() { Name = parameter.Key, Uid = parameter.ID };
            panel.Children.Add(cmbLabel);
            panel.Children.Add(cmb);

            addComboBoxItems(parameter.ParameterValue, cmb);
        }
    }

    public static void addComboBoxItems(IEnumerable<ParameterValue> parameterValues, System.Windows.Controls.ComboBox cmb)
    { 
        foreach (ParameterValue value in parameterValues)
        {
            ComboBoxItem item = new ComboBoxItem()
            {
                Content = value.Caption,
                Uid = value.ID,
                Name = value.Key
            };
            cmb.Items.Add(item);
        }
    }

}

解决方法:

我不确定,但我认为问题是此行:

String selectedItemId = ((e.Source as System.Windows.Controls.ComboBox).SelectedItem as ComboBoxItem).Uid;

当您清除组合框的项目时,

e.Source为System.Windows.Controls.ComboBox).SelectedItem作为ComboBoxItem)为null.

因此,当您尝试获取空项目的Uid时,它将引发异常.

上一篇:如何在JavaFX中防止在SPACE键上关闭AutoCompleteCombobox popupmenu


下一篇:Failed to load ApplicationContext