【问题标题】:Combobox interaction issue组合框交互问题
【发布时间】:2017-08-23 11:38:20
【问题描述】:

我有一个 wpf 用户控件库,其中包含三个组合框(cmbBox1、cmbBox2 和 cmbBox3)。最初,组合框 2 和 3 被禁用。用户首先从 cmbBox1 中选择和项目。根据用户选择填充 cmbBox2 和 cmbBox3 中的项目。我的代码第一次运行良好。但是,当用户更改 box1 中的选择时,它会中断。这是我的代码:

private void cmbBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.cmbBox2.Items.Clear();
    this.cmbBox3.Items.Clear();

    //this.cmbBox2.ItemsSource = new object[] {};
    //this.cmbBox3.ItemsSource = new object[] {};

    string Box1String = cmbBox1.SelectedItem.ToString();

    if (this.cmbBox1.SelectedIndex == 0)
    {
        this.cmbBox2.ItemsSource = new object[] { "Item2_0"};
        this.cmbBox3.ItemsSource = new object[] { "Item3_0"};
    }
    else if (this.cmbBox1.SelectedIndex == 1)
    {
        this.cmbBox2ItemsSource = new object[] { "Item2_1"};
        this.cmbBox3.ItemsSource = new object[] { "Item3_1"};
    }

}

private void cmbBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    string Box2String = cmbBox2.SelectedItem.ToString();

    //Rest of the code//

}

当表单打开时,代码运行良好。现在进行选择后,如果用户返回并更改组合框 1 中的选择,则代码会因错误而崩溃:

System.NullReferenceException: '对象引用未设置为对象的实例。'

在 Combobox2 SelectionIndexChanged 事件的第一行。我认为这是因为当combobox1 中发生任何更改时,我正在清除Combobox2 和combobox3 中的项目。因此,我尝试将这些行注释掉,但也没有成功。我目前不知道为什么会发生这种情况,非常感谢任何帮助。

【问题讨论】:

    标签: wpf combobox


    【解决方案1】:

    我找到了解决问题的解决方案。我所做的是在组合框 1 的选择更改事件中断开组合框 2 和 3 的 selectionchanged 事件。稍后,在我绑定组合框 2 和 3 的项目后,我再次将该功能分配给选择更改事件。这是有效的代码。

    private void cmbBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //this.cmbBox2.Items.Clear();
        //this.cmbBox3.Items.Clear();
    
        //this.cmbBox2.ItemsSource = new object[] {};
        //this.cmbBox3.ItemsSource = new object[] {};
    
        string Box1String = cmbBox1.SelectedItem.ToString();
        this.cmbBox2.SelectionChanged -= new SelectionChangedEventHandler(cmbBox2_SelectedIndexChanged);
        this.cmbBox3.SelectionChanged -= new SelectionChangedEventHandler(cmbBox3_SelectedIndexChanged);
    
        if (this.cmbBox1.SelectedIndex == 0)
        {
            this.cmbBox2.ItemsSource = new object[] { "Item2_0"};
            this.cmbBox3.ItemsSource = new object[] { "Item3_0"};
        }
        else if (this.cmbBox1.SelectedIndex == 1)
        {
            this.cmbBox2ItemsSource = new object[] { "Item2_1"};
            this.cmbBox3.ItemsSource = new object[] { "Item3_1"};
        }
        this.cmbBox2.SelectionChanged += new SelectionChangedEventHandler(cmbBox2_SelectedIndexChanged);
        this.cmbBox3.SelectionChanged += new SelectionChangedEventHandler(cmbBox3_SelectedIndexChanged);
    }
    
    private void cmbBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Box2String = cmbBox2.SelectedItem.ToString();
    
        //Rest of the code//
    
    }
    
    private void cmbBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Box3String = cmbBox3.SelectedItem.ToString();
    
        //Rest of the code//
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2011-03-30
      相关资源
      最近更新 更多