WPF ComboBox Binding To Object On Initial Load?

SRM, if I understand correctly your problem is binding your comboBox to a collection of objects rather than a collection of values types ( like string or int- although string is not value type). I would suggest add a two more properties on your combobox.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I have a combo box that is bound to a list of model objects. I've bound the combo box SelectedItem to a property that is the model type. All of my data binding works beautifully after the window has been loaded.

The SelectedItem is set properly and I'm able to save the object directly with the repository. The problem is when the window first loads I initialize the SelectedItem property and my combobox displays nothing. Before I moved to binding to objects I was binding to a list of strings and that worked just fine on initialization.

I know I'm missing something but I can't figure it out. Thanks in advance for any guidance you can provide. (One note about the layout of this page.

The combo boxes are actually part of another ItemTemplate that is used in a ListView. The ListView is bound to an observable collection in the main MV. Each item of this observable collection is itself a ModelView.

It is that second ModelView that has the SelectedItem property. ) Here is my Model: public class DistributionListModel : Notifier, IComparable { private string m_code; private string m_description; public string Code { get { return m_code; } set { m_code = value; OnPropertyChanged("Code"); } } public string Name { get { return m_description; } set { m_description = value; OnPropertyChanged("Name"); } } #region IComparable Members public int CompareTo(object obj) { DistributionListModel compareObj = obj as DistributionListModel; if (compareObj == null) return 1; return Code. CompareTo(compareObj.

Code); } #endregion } Here the pertinent code in my ModelView: public MailRoutingConfigurationViewModel(int agencyID) : base() { m_agencyID = agencyID; m_agencyName = DataManager.QueryEngine. GetAgencyName(agencyID); IntializeValuesFromConfiguration(DataManager.MailQueryEngine. GetMailRoutingConfiguration(agencyID)); // reset modified flag m_modified = false; } private void IntializeValuesFromConfiguration(RecordCheckMailRoutingConfiguration configuration) { SelectedDistributionList = ConfigurationRepository.Instance.

GetDistributionListByCode(configuration. DistributionCode); } public DistributionListModel SelectedDistributionList { get { return m_selectedDistributionList; } set { m_selectedDistributionList = value; m_modified = true; OnPropertyChanged("SelectedDistributionList"); } } And finally the pertinent XAML: c# wpf data-binding mvvm combobox link|improve this question asked Mar 16 '11 at 20:55SRM1,093129 100% accept rate.

SRM, if I understand correctly your problem is binding your comboBox to a collection of objects rather than a collection of values types ( like string or int- although string is not value type). I would suggest add a two more properties on your combobox I am assuming here that DistributionListModel objects are identified by their Code. The two properties I added SelectedValuePath and SelectedValue help the combobox identify what properties to use to mark select the ComboBoxItem by the popup control inside the combobox.

SelectedValuePath is used by the ItemSource and SelectedValue by for the TextBox.

This looks promising as well. You are correct in your assumption that my DistributionListModel objects are identified by their Code (and, in fact, it is the Code that is ultimately persisted). – SRM Mar 18 '11 at 1:28 Perfect!

Thank you sir for the correct answer. – SRM Mar 18 '11 at 8:43.

Don't call your IntializeValuesFromConfiguration from the constructor, but after the load of the view. A way to achieve that is to create a command in your viewmodel that run this method, and then call the command in the loaded event. With MVVM light toolkit, you can use the EventToCommand behavior... don't know mvvm framework you are using but there would probably be something like this.

It's actually a roll your own framework. I started it before I heard of the MVVM toolkit. A lot of the classes I created mirror the functionality in the MVVM toolkit though (only so many ways to solve the same problem) and my patterns closely resemble those used in the toolkit.

Notifier, for example, is the same as DependencyObject (something I just learned yesterday). I think your idea is sound and will give it a shot. Thanks!

– SRM Mar 16 '11 at 21:53 @SRM: whether you use your own Notifer class or MVVM Light toolkit and its ViewModelBase, the actual thing that make it works is the INotifyPropertyChanged... not the dependencyobject (which also implement this interface). I don't see any use of using DependencyObject in Mvvm ViewModel layer – Steve B Mar 16 '11 at 21:55 Yes, I know. DependencyObject is just a wrapper for INotifyPropertyChanged.

It has debug support to check if the property name is correct, but in essence it gives you the OnPropertyChanged, which is overridable. It is that functionality that is in Notifier - hence the name "Notifier" :). INotifyPropertyChanged is just an interface also, not a concrete class, so I don't have to use any of those if I don't want to - I just implement the interface.

The rest is to make the programmers life easier (and reduce bugs). – SRM Mar 16 '11 at 21:57 I highly suggest you to take a look at MVVM Light toolkit. What I love in this toolkit is its simplicity.

There is only a few class, but with everything I need in a mvvm app. – Steve B Mar 16 '11 at 22:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions