开发者

Using MVVM to Solve Combobox Interaction

开发者 https://www.devze.com 2023-04-09 11:32 出处:网络
I\'m new to WPF and I haven\'t used MVVM yet but I think I\'m in a situation where it might help. In my program I\'ve got several comboboxes that all have the same comboboxitems and when the user ma

I'm new to WPF and I haven't used MVVM yet but I think I'm in a situation where it might help.

In my program I've got several comboboxes that all have the same comboboxitems and when the user makes a selection in one of the comboboxes the selected combobo开发者_StackOverflow中文版xitem gets disabled in the other comboboxes. (i.e. If the user has selected the comboboxitem with value 'a' in combobox #1 and selected the comboboxitem with value 'b' in combobox #2 then in remaining comboboxes both the comboboxitems with values 'a' and 'b' are disabled)

Anyway, I'm having trouble doing this programmatically in the code-behind and I was hoping someone could describe how to approach this problem using MVVM.

Thanks MG


Here is one of the best primers on MVVM applied to WPF, with first rate code.

It isn't a quick read, and don't get frustrated if even seemingly simple things take some time to grok.

To answer your question more directly, you use MVVM to make data binding work (not to mention your logic testable). So for a ComboBox, you firstly supply it with data. probably using an ObservableCollection which has support for data binding in it. You can synchronize the Selected Item(s) in the ComboBox(es) to a property(ies) in your view model, and change the contents of one based on a change in the Selected Item.

Suggest you read that article and work through some code, then follow up with some more targeted questions using code.

HTH,
Berryl

0

精彩评论

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