【问题标题】:bind one combobox to another in wpf在 wpf 中将一个组合框绑定到另一个组合框
【发布时间】:2015-10-19 16:52:09
【问题描述】:

我有三个组合框。我从数据库中获取每个组合框的值,每个组合框都依赖于另一个。目前,我正在填充 SelectionChanged() 事件的组合框。 我在 MainWindow 的构造函数中填充第一个组合框。

因此,当我从第一个组合框中选择一个值时,将调用 selectionchanged 事件。然后填充第二个组合框。当我从第二个组合框中选择一个值时,第三个组合框会在 selectionChanged 事件中填充。从第三个组合框中选择值后,我正在填充数据网格。

当我从第三个组合框选择值后尝试更改第一个组合框的值时,出现错误。

对象引用未设置为对象的实例!

我正在尝试将组合框相互绑定,但对于 wpf 来说,我想不出一种方法来做到这一点。

我附上了组合框的代码供参考。

 public MainWindow()

 {
         InitializeComponent();
         Fill_Combo();
 }

   //adding values in first combo box 
  void Fill_Combo()
    {
        try
        {
            string query = "select column_name from table_name";               
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            OracleCommand command = new OracleCommand();
            command.Connection = conn;
            command.CommandText = query;
            OracleDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                String str = reader.GetString(0);
                //source_name is name of combobox1  
                source_name.Items.Add(str);
            }
            reader.Dispose();                               
        }
        catch (Exception e)
        {
            MessageBox.Show(e.StackTrace + " :: in fill_combo");
        }
    } 

   // adding value in second combo box based on value in first combobox 
   private void source_name_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {          
        string query = "select column from table where source_name='" + source_name.SelectedItem.ToString() + "'";

        try
        {
            OracleConnection connection = new OracleConnection(connectionString);
            connection.Open();
            OracleCommand command = new OracleCommand();
            command.Connection = connection;
            command.CommandText = query;
            OracleDataReader reader = command.ExecuteReader();
            //clearing previous values in combobox2
            //target_box is the name of second combo box
            target_box.Items.Clear();
            while (reader.Read())
            {
                String str = reader.GetString(0);
                //adding values in second combobox
                target_box.Items.Add(str);
            }

            reader.Dispose();                                            
            connection.Close();

        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message + "source_name namespace SelectionChangedEventArgs chanegs");
        }
    }


   private void target_box_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            if (target_box.SelectedItem.ToString() != null && target_box.SelectedItem.ToString() != "Target system")
            {                   
                string query = "select columnname from map_pair_attributes where source_name='" + source_name.SelectedItem.ToString() + "'"
                      + "and target_name = '" + target_box.SelectedItem.ToString() + "'";
                OracleConnection connection = new OracleConnection(connectionString);
                connection.Open();
                OracleCommand command = new OracleCommand();
                command.Connection = connection;
                command.CommandText = query;
                OracleDataReader reader = command.ExecuteReader();
                attribute_box.Items.Clear();
                while (reader.Read())
                {
                    String str = reader.GetString(0);
                    //attribute box is name of third combobox
                    attribute_box.Items.Add(str);
                }

                reader.Dispose();
                connection.Close();
                connection.Dispose();


            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("in target box selection changed!" + ex.StackTrace + ex.Message);
        }
    }        

        //atributebox is name of third combobox 
         private void attribute_box_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {               
           String query = "select column_name from table where source_name='" + source_name.SelectedItem.ToString() + "' and target_name ='" +
                target_box.SelectedItem.ToString() + "' and attribute_name = '" + attribute_box.SelectedItem.ToString() + "'";

            OracleConnection conn = new OracleConnection(connectionString);
            conn.Open();
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(query, conn);
            da.Fill(ds, "table_name");
            grid1.ItemsSource = ds.Tables["table_name"].DefaultView;
              conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("attribute box selection changed!" + ex.Message);

        }
    }

.XAML 文件

   <ComboBox x:Name="target_box" HorizontalAlignment="Left" Margin="179,49,0,0" VerticalAlignment="Top" Width="120" 
        IsEditable="true" IsReadOnly="True" Text="Target system" 
        Focusable="True" SelectionChanged="target_box_SelectionChanged" />
                <ComboBox x:Name="source_name" HorizontalAlignment="Left" Margin="19,49,0,0" VerticalAlignment="Top" Width="120" 
        IsEditable="true" IsReadOnly="True" Text="Source system" Focusable="True" SelectionChanged="source_name_SelectionChanged"/>
                <ComboBox x:Name="attribute_box" HorizontalAlignment="Left" Margin="363,49,0,0" VerticalAlignment="Top" Width="120"
        IsEditable="true" IsReadOnly="True" Text="Attribute" Focusable="True" SelectionChanged="attribute_box_SelectionChanged" />

有人可以帮我解决这个问题吗? 我认为绑定可以解决问题,但我不知道如何绑定组合框。

谢谢!

【问题讨论】:

  • 互联网上关于 WPF 数据绑定的信息和示例数不胜数。在发布此类问题之前,您应该进行一些研究。
  • 我找到了一些示例并实现了它,但我找到了绑定单个组合框的示例。我在绑定多个组合框方面没有找到太多帮助。我现在被这个问题困扰了很多天。我还在网上搜索这个。正如我在问题中已经提到的那样,我是 WPF 的新手,对绑定也不太了解。不过还是谢谢!
  • 您可以将两个 ComboBox 绑定到同一个源属性

标签: c# wpf winforms xaml combobox


【解决方案1】:

当您在ComboBox 中选择一个新项目时,ComboBox.SelectedItem 首先设置为空。

尝试在每个事件处理程序中添加对 ComboBox.SelectedItem = null 的检查,然后再尝试对对象调用 ToString()。

例如

if(source_name.SelectedItem == null) return;

然后替换您的 ToString 空检查

if (target_box.SelectedItem.ToString() != null

if (!string.IsNullOrEmpty(target_box.SelectedItem.ToString())

【讨论】:

  • 我以前试过这个。这是行不通的。虽然我应该在我的代码中添加这些条件。谢谢!
  • 嘿,我添加了这些条件,现在对我的代码及其工作进行了一些更改。我所做的更改是将组合框绑定到 observableCollection 列表及其工作!我不必做任何嵌套绑定或任何复杂的事情。谢谢!
猜你喜欢
  • 2012-07-24
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2022-01-11
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
相关资源
最近更新 更多