[英]Strange behavior binding WPF combobox
我無法綁定到組合框的text屬性。 在我選擇組合框中的東西之前,它似乎沒有綁定。 然后它工作正常。
以下是測試應用程序的代碼:
視圖
<ComboBox ItemsSource="{Binding ListItems}"
Text="{Binding Test}" />
視圖模型
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")); }
}
}
但是,當我顛倒可觀察集合項的順序時,一切正常。
ListItems.Add("South");
ListItems.Add("Southwest");
這里發生了什么?
text屬性不能像這樣工作。 閱讀本文檔: http : //msdn.microsoft.com/en-us/library/system.windows.controls.combobox.text.aspx
像hameleon86所建議的那樣使用selecteditem。
我認為如果你顛倒你的集合的順序可能是有效的,因為Text屬性默認采用集合的第一項
我想你可能想做:
PropertyChanged(this,new PropertyChangedEventArgs(“ListItems”));
插入元素后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.