但MVVM模式下,就不那麼方便了,需要繫結SelectedItem,例如:
1:
2:<ComboBoxItemsSource={BindingMonths}3:SelectedItem={BindingMonth,Mode=TwoWay}/ 似乎可行,但如果是預設載入第一項的話,這個SelectedItem就應該是繫結源更新以後的ItemSource.FirstOrDefault(),為何要雙向繫結呢?有一點不太爽。有沒有其它辦法呢?很簡單,繼承ComboBox,寫一個控制元件,讓它預設選中第一項,就解決這個問題了,不用繫結SelectedItem。 <ComboBoxItemsSource={BindingMonths}/ 自定義ComboBox控制元件程式碼如下,也非常簡單,思路就是ItemSource更新以後就自動預設選中第一行即可:1:publicclassCustomComboBox:ComboBox2:{3:///<summary4:///Initializesanewinstanceofthe<seecref=CustomComboBox/class.5:///</summary6:publicCustomComboBox()7:{8:Loaded+=ComboBoxLoaded;9:SelectionChanged+=ComboBoxSelectionChanged;10:}11:12:voidComboBoxLoaded(objectsender,RoutedEventArgse)13:{14:SetSelectedItem();15:}16:17:voidComboBoxSelectionChanged(objectsender,SelectionChangedEventArgse)18:{19:SelectedItem=e.AddedItems.Count0?e.AddedItems[0]:null;20:}21:22:publicnewobjectSelectedItem23:{24:get{return(object)GetValue(SelectedItemProperty);}25:set{SetValue(SelectedItemProperty,value);}26:}27:28:publicnewstaticreadonlyDependencyPropertySelectedItemProperty=29:DependencyProperty.Register(SelectedItem,typeof(object),typeof(CustomComboBox),30:newPropertyMetadata((o,e)=((CustomComboBox)o).SetSelectedItem()));31:32:privatevoidSetSelectedItem()33:{34:if(Items.Count0)35:base.SelectedIndex=0;36:37://varvalue=SelectedItem;38://if(Items.Count0&&value!=null)39://{40://base.SelectedIndex=Items.IndexOf(value);41://}42:}43:44:protectedoverridevoidOnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgse)45:{46:base.OnItemsChanged(e);47:SetSelectedItem();48:}49:} 如果你覺得程式碼好用就拿過去,否則請分享更好的程式碼。
但MVVM模式下,就不那麼方便了,需要繫結SelectedItem,例如:
1:
2:<ComboBoxItemsSource={BindingMonths}3:SelectedItem={BindingMonth,Mode=TwoWay}/ 似乎可行,但如果是預設載入第一項的話,這個SelectedItem就應該是繫結源更新以後的ItemSource.FirstOrDefault(),為何要雙向繫結呢?有一點不太爽。有沒有其它辦法呢?很簡單,繼承ComboBox,寫一個控制元件,讓它預設選中第一項,就解決這個問題了,不用繫結SelectedItem。 <ComboBoxItemsSource={BindingMonths}/ 自定義ComboBox控制元件程式碼如下,也非常簡單,思路就是ItemSource更新以後就自動預設選中第一行即可:1:publicclassCustomComboBox:ComboBox2:{3:///<summary4:///Initializesanewinstanceofthe<seecref=CustomComboBox/class.5:///</summary6:publicCustomComboBox()7:{8:Loaded+=ComboBoxLoaded;9:SelectionChanged+=ComboBoxSelectionChanged;10:}11:12:voidComboBoxLoaded(objectsender,RoutedEventArgse)13:{14:SetSelectedItem();15:}16:17:voidComboBoxSelectionChanged(objectsender,SelectionChangedEventArgse)18:{19:SelectedItem=e.AddedItems.Count0?e.AddedItems[0]:null;20:}21:22:publicnewobjectSelectedItem23:{24:get{return(object)GetValue(SelectedItemProperty);}25:set{SetValue(SelectedItemProperty,value);}26:}27:28:publicnewstaticreadonlyDependencyPropertySelectedItemProperty=29:DependencyProperty.Register(SelectedItem,typeof(object),typeof(CustomComboBox),30:newPropertyMetadata((o,e)=((CustomComboBox)o).SetSelectedItem()));31:32:privatevoidSetSelectedItem()33:{34:if(Items.Count0)35:base.SelectedIndex=0;36:37://varvalue=SelectedItem;38://if(Items.Count0&&value!=null)39://{40://base.SelectedIndex=Items.IndexOf(value);41://}42:}43:44:protectedoverridevoidOnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgse)45:{46:base.OnItemsChanged(e);47:SetSelectedItem();48:}49:} 如果你覺得程式碼好用就拿過去,否則請分享更好的程式碼。