1

I'm binding my combobox to a list here, the SelectedItem is also bound to CurrentCode property in code behind. Now every thing is displayed well and the selected item is set as expected. But let's I'm changing the value of the CurrentCode by clicking a button. Why the combobox doesn't get updated?

<ComboBox ItemsSource="{Binding Path=Codes}" SelectedItem="{Binding CurrentCode, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Mode=OneTime}"/> public partial class SettingsWindow { public List<string> Codes { get; set; } public string CurrentCode { get { return Building.Code; } } public SettingsWindow() { InitializeComponent(); Codes = new List<string> {"ACI Code", "BS Code"}; DataContext = this; } private void OK_OnClick(object sender, RoutedEventArgs e) { Building.Code = "BS Code; } } 

2 Answers 2

1

If you're in the code-behind, you could just give the ComboBox a name and access it directly.

If you want to make it work with the property (ideally by moving it into its own class, in MVVM fashion, and setting the DataContext of your Window to that "view model" class), you'll need to implement INotifyPropertyChanged:

public partial class SettingsWindow : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private void OK_OnClick(object sender, RoutedEventArgs e) { Building.Code = "BS Code; OnPropertyChanged("CurrentCode"); } ... } 
Sign up to request clarification or add additional context in comments.

Comments

1

You should implement INotifyPropertyChanged and rise the PropertyChanged event when you change the property backing field http://msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.