I'm having trouble binding to the text property of a combobox. It seems like it doesn't bind until I select something in the combobox. Then it works fine.
Here is the code straight from a test app:
View
<ComboBox ItemsSource="{Binding ListItems}"
Text="{Binding Test}" />
ViewModel
class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> ListItems { get; set; }
public ViewModel()
{
ListItems = new ObservableCollection<string>();
ListItems.Add("Southwest");
ListItems.Add("South");
}
public string Test
{
get { return "South"; }
set { PropertyChanged(this, new PropertyChangedEventArgs("Test")); }
}
}
However, when I reverse the order of the observable collection items, everything works fine.
ListItems.Add("South");
ListItems.Add("Southwest");
What's going on here?
The text property doesn't work like this. Read this document: http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox.text.aspx
Like suggested by hameleon86 use the selecteditem instead.
I think it Works if you reverse the order of your collection maybe because the Text property take the first item of the collection by default
I think you might want to do :
PropertyChanged(this, new PropertyChangedEventArgs("ListItems"));
After you inserted the element.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.