0

how to bind a ComboBox to a list of string here is my list :

  public ObservableCollection<string> m_Category = 
                                              new ObservableCollection<string>();

 <ComboBox x:Name="MyComboBox" Height="Auto" Width="Auto"  
        ItemsSource="{Binding m_Category, NotifyOnTargetUpdated=True,Mode=TwoWay,
        UpdateSourceTrigger=PropertyChanged}" SelectedIndex ="0"  
        SelectionChanged ="MyComboBox_SelectionChanged"/>

Edit

Plz note that my comboBox is inside a DataTemplate Thks

MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 1
    What's the `DataContext` of your `ComboBox`? your `m_Category` should be a Property of that `DataContext`. – King King Dec 04 '13 at 13:54
  • how to define a datacontext – MRebai Dec 04 '13 at 14:06
  • what's the difference between DataContext and itemsource – MRebai Dec 04 '13 at 14:10
  • `DataContext` can be passed down from parent to children in the visual tree, so you can set the `Window.DataContext`, your comboBox will have the same `DataContext` unless you explicitly assign a new one for it. `DataContext` is used to **implicitly** set the `Source` for all the bindings, that's why your `Binding` doesn't have any `Source` set, it just need to specify other info such as `Path`, ... `ItemsSource` is just some access point for you to put data into your comboBox to display the data. I thought you already know what DataContext is? – King King Dec 04 '13 at 14:16

2 Answers2

1

you should not use binding here in your ItemsSource because you don't have a datacontext set here just in code behind do this (after m_Category filling )

MyComboBox.ItemsSource =  m_Category ; 

Otherwise you should create a class contains a property like this and your bind will work

 public class MyDataContext
        {
    ObservableCollection<string> m_Category = 
                                              new ObservableCollection<string>();
        public  ObservableCollection<string>   M_Category 
    { get;set}
      }

//Change your bind like this

 <ComboBox x:Name="MyComboBox" Height="Auto" Width="Auto"  
        ItemsSource="{Binding M_Category, NotifyOnTargetUpdated=True,Mode=TwoWay,
        UpdateSourceTrigger=PropertyChanged}" SelectedIndex ="0"  
        SelectionChanged ="MyComboBox_SelectionChanged"/>

in your main window you can do something like this

 public MainWindow()
    {
        InitializeComponent();
         MyDataContext myDataContext =  new  MyDataContext(); 
           //for example here  
            For(i=0;i<100;i++)
                    myDataContext.M_category.Add(yourItem)
        this.DataContext =  myDataContext ; 
    }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

First of all check if you set a DataContext to your ComboBox otherwise the binding won't work.

Your XAML file should look something like this:

 <ComboBox Name="cbPropName" ItemsSource="{Binding Path=m_Category}" />

it should work, if you still have a problem take a look in this post

Community
  • 1
  • 1
Ofir
  • 5,049
  • 5
  • 36
  • 61
  • So how can i add a dataContext to the combobox – MRebai Dec 04 '13 at 14:04
  • Its quite basic question, if you aren't familiar with DataContext and how it's works, I'm suggesting you to read the next article: http://www.codeproject.com/Articles/321899/DataContext-in-WPF – Ofir Dec 04 '13 at 14:09