views:

17

answers:

1

When binding data to combobox, 3 items need to be set:

<ComboBox  ItemsSource="{Binding MyList}" SelectedItem="{Binding MyItem}"  DisplayMemberPath="MyName"   />

Say ItemSource is list of Country

I set itemsource to right source firstly, it is fine. Then I set selectedItem to specific Country object, but it's not working.

Looks like all need to be set when ItemSource is setting.

How to resolve this problem?

A: 

UPDATE WITH WORKING CODE

Make sure you enable two-way binding on the SelectedItem.

<ComboBox ItemsSource="{Binding Path=Countries, Mode=OneWay}" SelectedItem="{Binding Path=SelectedCountry, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="12,28,0,0" Name="comboBox1" VerticalAlignment="Top" Width="267" />

Here is what your context will look like:

public partial class MainPage : UserControl, INotifyPropertyChanged
{
    public MainPage() 
    { 
        InitializeComponent();
        this.Countries = new ObservableCollection<string> { "USA", "CAN" };
        this.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<string> Countries { get; set; }

    private string _selectedCountry = null;
    public string SelectedCountry
    {
        get { return _selectedCountry; }
        set
        {
            _selectedCountry = value;
            if( this.PropertyChanged != null )
                this.PropertyChanged( this, new PropertyChangedEventArgs( "SelectedCountry" ) );
        }
    }

    private void button1_Click( object sender, RoutedEventArgs e )
    {
        MessageBox.Show( "Selected Country: " + this.SelectedCountry );
    }

}
j0rd4n
BTW, the base.OnPropertyChanged assumes you derive from a base ViewModel class. Otherwise, you'll call the property changed event from INotifyPropertyChanged.
j0rd4n
Thank you. I did exactly as you did. but I still can't display selectedCountry. I use IEnumerable<T> for Country List. I load the list in app.xmal, not in specific VM because it is supposed for all vms.
KentZhou
Find out something interesting: hard code for testing like//before: SelectedItem is nullthis.xCountry.SelectedItem = ((AddressViewModel)ctx).SelectedCountry;//After: SelectedItem is still nullDon't understand.
KentZhou
You can't use IEnumerable<T> for your Country source. You have to use an ObservableCollection (or some collection that implements INotifyPropertyChanged). That is what broadcasts to the binding notifying that a change in the collection has been made.
j0rd4n
as you put in xaml, ItemSource is oneway binding. TwoWay is for selectedItem. I can display list with no problem. My problem is SelectedItem. It looks like you must pick up the selecteditem from the list, and can't assign it to a separated instance even the class is same. Any idea?
KentZhou
I changed the code in my post. The above code is a working example. I got it to work with both an ObservableCollection and IEnumerable. Remember, you might have to leave focus on the combobox for the selection to be updated. Many of the bindings won't be updated until focus is lost.
j0rd4n