CHANDRA
CHANDRA

Reputation: 4928

exception in add combobox item

I tried to add item dynamically to ComboBox.

but it throw the exeption "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead".

enter image description here

namespace Trainning

{

public partial class ComboBox : Window
{
    int intex_count;

    public ComboBox()
    {
        this.InitializeComponent();         

        add_items();


    }

    public List<object> add_items()
    {
        List<object> items = new List<object>();

        items.Add("chandru");
        items.Add(83);

        com_add_remove.ItemsSource = items;
        com_add_remove.SelectedIndex = 0;

        return items;
    }
 private void btn_add_Click(object sender, RoutedEventArgs e)
    {

        com_add_remove.Items.Add(txt_item.Text);
         intex_count = com_add_remove.Items.Count;
        com_add_remove.SelectedIndex = intex_count - 1;

    }

Upvotes: 0

Views: 728

Answers (2)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

Once you have bound your combobox to some sort of data source after that you should only change the data source.

You need to declare the List<object> items = new List<object>(); in your class and just add and remove items from the list. Your combobox will automatically be updated.

public partial class ComboBox : Window
{
  int intex_count;
  List<object> items;
  public ComboBox()
  {
    this.InitializeComponent();         



    //key_value();

    TextBox tb = new TextBox();
    tb.Height = 50;
    tb.Width = 100;
    tb.TextAlignment = TextAlignment.Center;
    LayoutRoot.Children.Add(tb);
    tb.Text = "Dynamic TextBox";
    tb.Margin = new Thickness(0, 145, 87, 0);
    tb.VerticalAlignment = VerticalAlignment.Top;
    tb.HorizontalAlignment = HorizontalAlignment.Right;
    tb.Padding = new Thickness(15, 15, 15, 15); //to center the textbox's text  

    items = new List<object>();
    add_items();
    com_add_remove.ItemsSource = items;
    com_add_remove.SelectedIndex = 0;
}

public List<object> add_items()
{
    //List<object> items = new List<object>();

    items.Add("chandru");
    items.Add(83);        

    return items;
}

private void btn_add_Click(object sender, RoutedEventArgs e)
{

     items.Remove(txt_item.Text);
     intex_count = items.Count;

}

Upvotes: 1

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

Your problem is that when you use ItemsSource, you can't also add items manually to the same component. So you have to either use ItemsSouce or add and remove items manually.

I would go with ItemsSource, and to the following change:

replace:

com_add_remove.Items.Add(txt_item.Text);

with:

items.Add(txt_item.Text);

That means you have to add items as a class variable instead of just a local variable inside the add_items method so that you can reference it from btn_add_Click as well.

Your other option is to change the add_items method so add items istead of using ItemsSource:

replace:

com_add_remove.ItemsSource = items;

with:

items.ForEach( i => com_add_remove.Items.Add(i));

Upvotes: 2

Related Questions