Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What i'm doing wrong?
Basic binding to Combobox ItemsSource.

What I have tried:

C#
C#
public partial class MainWindow : Window
    {
        ObservableCollection<string> _items = new ObservableCollection<string> { "a1", "a2" };


        public ObservableCollection<string> Items

        {

            get { return _items; }

            set { _items = value; }

        }
        public MainWindow()
        {
            InitializeComponent();
            var aa = a1;
        }
    }

XAML

XML
<StackPanel>
       <ComboBox Name="a1" ItemsSource="{Binding Items}">


       </ComboBox>
   </StackPanel>
Posted
Updated 10-Apr-19 21:11pm

read another codeproject article
Step by Step WPF Data Binding with Comboboxes[^]

Quote:
ItemsSource - is bound to the static resource array 'ColorListString' that we defined above as an application resource in the App.xaml file. This list of items is used to populate the Items collection of the ComboBox. This collection is used to generate the items that show up in the dropdown list.
DisplayMemberPath - is bound to the ValueString property of the ComboBoxItemString object, in the ItemsSource list. When an item is selected from the dropdown list, this is the value that is displayed to the user.
SelectedValuePath - is bound to ValueString property of the ComboBoxItemString object, in the ItemsSource list. When an item is selected from the dropdown list, this is the property used to get the value to set the SelectedItem value to.
SelectedValue - is bound using a property binding of "{Binding ColorString}". When an item is selected in the list, this is the property on the data object that is set to the value returned from the SelectedValuePath property.


XML
<pre><combobox itemssource="{StaticResource ColorListString}">
            DisplayMemberPath="ValueString" 
            SelectedValuePath="ValueString" 
            SelectedValue="{Binding ColorString}" /></combobox>
 
Share this answer
 
This answer is way after the posting and I hope you figured it out by now, but when you're binding without a source the default source is the DataContext property. In this example your DataContext property has not been set yet. The easiest fix in this example is to do this:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}


I personally do not recommend a view being it's own DataContext, but if you're going to do it you should also make it's exposed property you're binding to a DependencyProperty.
 
Share this answer
 
Hi.

You need to implement INotifyPropertyChanged on you viewmodel (class with your observable collection).

Once that is done you'll use the eventhandler provided by the interface to let your GUI know that whenever your collection is updated.

Typcally it looks like this.

public class YourClass : INotifyPropertyChagned
{
    private ObservableCollection<string> _collectionOfStrings;
    public ObservableCollection<string> CollectionOfStrings
    {
        get => _collectionOfStrings
        set
        {
            if (value != _collectionOfStrings)
            {
                _listOfStrings = value;
                OnPropertyChagned("CollectionOfStrings");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName =
        null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900