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 comboboxitem 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
Related
i want to immitate the behavior of multiselection of items (eg. in a list view) and their appearence in a PropertyGrid. So, obly the same properties are shown and values set in the grid are set on all multiselected properties.
I have created a listview with some datatemplates and have in another listview some data(with databinding). Now i want wo have a multiselection on the listview with the data and have the properties shown in the another listview like in a propertygrid.(explanation: The ProprtyGrid is still not available in WPF and if has not the flexibility of showing data the way i need it)
So, how do i need to prepare my data to be shown in the list propertygrid-multiselection-style? Is that even possible???
Greets,
Jürgen
I'm not sure if this question is still active, or what not. But I have found a solution that works for me - and I need somewhere to share it.
The first issue you are going to have solve is selecting the items, ie. multi-select. See my tip here that explains how to do that.
Next, I guess its basically binding to listview's selectedItem property and how you want to update your propertyGrid based on changes to the selectedItem prop.
Hope this helps.
Can one one please suggest me which one will be the best, fast and efficient method to fill a ListView in a WPF app.
From ObservableCollection or from DataSet?
Thanks.
I always go for ObservableCollection, I'd never use a DataSet bound directly to a control.
And if you are not changing the items in the collection, you should consider using List.
But with a little more info, maybe there would be good reason to use the dataset, but I don't think so.
I have a two column combobox, and a textbox, bound to xml data.
The textbox shows the equivalent of the comboboxes second column of the currently selected item.
I've bound the datacontext of the textbox to the SelectedItem in the combobox, which then updates if you select a row in the combobox. Now, I'd it so that if you type something into the textbox that corresponds to a value in the 2nd column of the combobox, it selects that row.
I realise this is slightly circular.
I've managed it before in winforms, by effectively suspending events when the CombobBox OnSelectedItemChanged fires and updates the textbox or OnTextChange fires and updates selectedItem.
The idea is that the user can either select an option from the combo, or if they know a short code (in this case, country ISO), they can just type it in and immediately see the appropriate country selected in the combobox.
Is it somehow possible to bind the selectedItem in the combobox to the textBox in addition to the underlying data (and indeed does that idea make any sense?), or possibly do some sort of two-way-binding between these elements?
I'm hoping there's a simpler solution than dependencyproperties-- ideally something purely in xaml, but appreciate as I'm new at WPF, I've no idea if this is even possible.
Thanks!
Mike
If you've got a ViewModel you can two-way bind each to the same property of that, and this is probably the best way to do it.
In particular I would like to know how to bind the 'SelectionChanged' event of the dataGrid to a Command on my Viewmodel.
Since DataGrid doesnt have a Command property, how do I call a modelView command as in MVVM fashion? I dont mind using a delegate on the code behind XAML, if I knew how to do that...
Since I am new to WPF I am very stuck on how to accomplish this. May someone please help me with this?
Kind Regards,
Kave
Thanks Cameron. I had discovered it first too, but I prefer not using 3rd Party libraries at this stage.
After many many hours, I have found this link that helped me solving the problem in a different way.
In fact, there is no need for a DataGrid to use commands because its not really executing an action such as a button. The 'SelectionChanged' event can be easily made talking to the modelview by exposing a "selectedItem" property in the modelView and bind it to the Datagrid's selectedItem. The following example does it with a combobox instead of a datagrid, but its exactly the same concept. However I recommend using the CollectionView instead and making sure to feed the datagrid with a ObservableCollection<> and not with a e.g. DataTable.
What is the easiest way to handle SelectedItem event with MVVM?
I've got a xaml TabControl and on one page, there are 3 RadioButtons each bound to a different property on the selected value of an adjacent ListView. After switching between selected items in the ListView, my radio buttons seem to forget they're bound and don't refresh.
So watching it in the debugger, when I switch to a new selected item, I see the non user code first checking the value on all 3 properties, then only the first two, and eventually only the first. However, if I change the tab and change back, it seems to give me another few uses.
The binding itself is fairly straightforward. a TwoWay binding of a bool property to IsChecked. It's 4 levels deep (Path=DataModel.Selected.A.B), but I have other things at the same depth that work fine.
Is this something people have heard about and know what might be going on? Or if the binding is somehow getting forgotten, is there a way to explicitly remind the xaml?
It is possible to manually update bindings like this:
TestCheckBox
.GetBindingExpression(CheckBox.IsCheckedProperty)
.UpdateTarget();
That being said, I don't have 100% confidence that this will correct your underlying issue. I haven't had this sort of issue before with WPF bindings, but I have had a couple weird issues with the tab control.
This is apparently somewhat of a known issue:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8eb8280a-19c4-4502-8260-f74633a9e2f2/
In short, a RadioButton (through .Net 3.5sp1) somehow kills bindings of other RadioButtons when when it's checked while trying to uncheck any other buttons. The simple fix (read: hack) is to assign each radiobutton a different GroupName and then they don't try to mess with eachother
Another way to resolve this issue is to fake up a list of properties in a ListBox and have the ListBoxItem template be a radiobutton.