0

There is two combobox in my WPF app.

<ComboBox x:Name="itemsList" HorizontalAlignment="Left" Margin="10,90.767,0,0" VerticalAlignment="Top" Width="215" IsEditable="True" Height="23"/>


<ComboBox x:Name="pSize" HorizontalAlignment="Left" Margin="230,90.767,0,0" VerticalAlignment="Top" Width="109.538" Height="23" IsEditable="True" />

First combobox fetch items from database with this function which i initialize when app loads

  void getAllItems()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT  DISTINCT item_name FROM items", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            String items = dr.GetString(0);
            itemsList.Items.Add(items);
        }


        conn.Close();
    }

I want to bind my second combobox "pSize" with "itemlist". I want to get the value from itemlist and pass to the method which generate items for "pSize" from database according to the parameters.I tried to bind it but not working.

3 Answers3

1

If you want to use WPF, then you should use it properly and not like WinForms like you are currently doing. In WPF, we don't programmatically add items to UI controls. Instead we add items to a collection and then data bind that to the UI controls. In this way, we can data bind the same collection to as many UI controls as we like. So, try something like this:

private ObservableCollection<string> items = new ObservableCollection<string>();
public ObservableCollection<string> Items
{
    get { return items; }
    set { items = value; NotifyPropertyChanged("Items"); }
}

...

void getAllItems()
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT  DISTINCT item_name FROM items", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cmd.ExecuteNonQuery();
    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        String items = dr.GetString(0);
        Items.Add(items);
    }
    conn.Close();
}

...

<ComboBox ItemsSource="{Binding Items}" />

Don't forget to set the DataContext of the UserControl or Window to an instance of the class where the Item property is declared and populated. You would probably also benefit from reading the Data Binding Overview page on MSDN.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • With this method i have to change many thing in my application, can u tell easy approach like i told u in my question. I just want to pass parameter to a method when itemList change. – Burhan aka SLIM SHADY Nov 20 '14 at 14:51
  • 1
    *With this method i have to change many thing in my application*... that's the point! If you don't want to program correctly, then you'll have to ask someone else. I will only show you how to program the proper WPF way. – Sheridan Nov 20 '14 at 15:56
0

You'd better create a model, and apply it to each Combobox. Now you can code you bussiness logic to generate pSize items in this model using data from database.

You can also use LINQ to apply some method to each List element and bind result (List) with you pSize.

List<T> newList = items.ForEach(item=> someMethod(item));

where T is string, in your case (as I see).

Also look at this and this threads.

Community
  • 1
  • 1
Artem E
  • 369
  • 1
  • 4
  • 19
0

Easiest way, no need to make a list. I found it my self:

private void itemsList_DropDownClosed(object sender, EventArgs e)
{
   MessageBox.Show(itemsList.Text) 
}