开发者

MVVM Question about default values in combo boxes

开发者 https://www.devze.com 2023-01-26 13:42 出处:网络
I have a combo box on a view that will have a default of \"Active\" when the form is loaded. Whose job is it to set the combobox to开发者_JAVA百科 \"Active\", the code behind in the loaded method or t

I have a combo box on a view that will have a default of "Active" when the form is loaded. Whose job is it to set the combobox to开发者_JAVA百科 "Active", the code behind in the loaded method or the View Model?

Bill


I'd say it's the ViewModel's job. There are at least 2 approaches to do it:

  • bind the SelectedItem of the combo to a property of the ViewModel, and set this property to the value you want
  • obtain the default view for the collection (CollectionViewSource.GetDefaultView) and set the current item with the MoveCurrentTo method (don't forget to set IsSynchronizedWithCurrentItem to true on the ComboBox)
0

精彩评论

暂无评论...
验证码 换一张
取 消