WPF ComboBox SelectedItem Binding not working

I have the following ComboBox that gets populat from an enumeration:

<ComboBox Name="cmbProductStatus" ItemsSource="{Binding Source={StaticResource statuses}}" SelectedItem="{Binding Path=Status}" /> 

Note that the DataContext is set to code.

This is not even a two-way binding, I have a default value for Product.Status, but it will never be selected.

Update

I was asked to put the code for my Status property.

 public class Product { //some other propertties private ProductStatus _status = ProductStatus.NotYetShipped; public ProductStatus Status { get { return _status; } set { value = _status; } } } public enum ProductStatus { NotYetShipped, Shipped }; 
0
c # wpf binding
source share
2 answers

The status property should notify the change, and your product class must implement the INotifyPropertyChanged interface.

Here you have the MVVM Light code snippet for the property; -)

 /// <summary> /// The <see cref="MyProperty" /> property name. /// </summary> public const string MyPropertyPropertyName = "MyProperty"; private bool _myProperty = false; /// <summary> /// Gets the MyProperty property. /// TODO Update documentation: /// Changes to that property value raise the PropertyChanged event. /// This property value is broadcasted by the Messenger default instance when it changes. /// </summary> public bool MyProperty { get { return _myProperty; } set { if (_myProperty == value) { return; } var oldValue = _myProperty; _myProperty = value; // Remove one of the two calls below throw new NotImplementedException(); // Update bindings, no broadcast RaisePropertyChanged(MyPropertyPropertyName); // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true); } } 
+1
source share

Linking to a ComboBox is a bit more complicated. Make sure that the items source is loaded when assigning the DataContext and that the item you assigned using SelectedItem matches == with one item in the ItemsSource.

+2
source share

All Articles