WPF listview, Load, Save & Add - wpf

I have a columned listview who's items I would like to store in an XML file.
What's the best way to go about loading, saving and adding items?

best way is to use MVVM :), that way your View simply represents the data in the UI. Actual DomainModel/BusinessObject resides outside your View.
And then you can use number of persistance methods:
XML Serialization
ORM (Object Relation Mapping), which will persist/save the BusinessObject into Database and back
Step by step way:
Create/Define your BusinessObject(DomainModel, i.e. Person class)
Use DataTemplate to Bind to Collection from your ListView
In your ViewModel, you can than say PersonCollection.Save/Load etc

Related

Is it correct to let the viewmodel add children to its view?

Im currently learning about the MVVM pattern in WPF. I think it is really cool but my question is: is it correct to use the viewmodel directly for appending children to its view?
For example lets assume we had a method which contains a loop which adds new rows to a grid when a button is clicked. Should my ViewModel only contain pure data or can it also contain logic for placing new elements on the field? As well as removing them.
is it correct to use the viewmodel directly for appending children to its view?
No. The view model shouldn't know about any view elements.
Should my ViewModel only contain pure data or can it also contain logic for placing new elements on the field? As well as removing them.
The former. The view model may expose a collection of data objects that the view happens to display in a Grid. Or some other kind of panel. The view model doesn't care about which.
You would typically use an ItemsControl in the view to display the items in a view model collection, i.e. the ItemsControl binds to the source collection and displays a visual representation of each item in a panel.

Dynamic column generation in WPF DataGrid

I have an object which has some 30 properties, depending on who is viewing the data, I need to present just 10-12 properties to him.
These properties will form the columns of the WPF datagrid. I am using .Net 4.0
However, all this will happen at run time. I need to do this is the MVVM way.
Any direction as to how to achieve this will be appreciated.
regards,
You have two options as far as I can see.
1) Expose a ViewableDetailsType enum property from your ViewModel that tell you what kind of view of the data you should show. In the view you can then create triggers in your DataGrid to set the Columns property of the grid to manually show the appropriate columns.
2) A better way would be to create wrapper, DTO type ViewModel objects for your underlying model object. One for each view of the object you want to expose. You then expose a collection of the appropriate wrapper objects to the view, and the DataGrid can use auto columns.
This is slightly more work, but it is truer to MVVM since the data hiding is taking place in the ViewModel, and can thus be tested.

Using Data sets in MVVM

What is the best practice to use data sets instead of an observable collection in MVVM to bind to grids. is it OK to have a property of type DataSet on the view model? How is the design time data set in this case?
One of the key concepts of the MVVM pattern is that the ViewModel is the "model of your view", it shapes your business model in such a way that it is easier to bind a UI (i.e. your View) to it.
You can certainly use a DataSet / DataTable to expose data from your ViewModel and bind it to your View. I don't see anything wrong with this approach. It is certainly valid MVVM!
Regarding design time data, it depends on how you are creating it. You cannot create a DataSet in XAML, so cannot use a XAML file within visual studio for your data. However, if you are programmatically creating design time data, i.e. within your ViewModel checking whether it is design-time, then creating data in your code, it will work just fine.

Displaying an XDocument as a WPF treeview using an MVVM ViewModel

I have XSD's held as data in a SQL Server XML, or oracle XMLtype field. I retrieve an XSD from the database as an XDocument. I want to display this XDocument in a WPF treeview. This is relatively straight forward if I bind the Xdocument directly to the treeview using an xmldataprovider and a hierarchicalDataTemplate.
However, my application is using the mvvm pattern and I would like to represent the xdocument in the ViewModel layer, which the treeview then binds to in a similar way to the Josh Smith article on binding the WPF treeview to ViewModels.
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
My goal is to select an XNode of the XSD (XDocument).
Any suggestions for how I could represent the Xdocument of an XSD in the ViewModel so that I can select an XNode?
If you're doing read-only operations, I'd recommend that you keep it simple and don't introduce new classes that provide no benefit. Just leave a comment that you should add a view model if you ever want to support editing the structure. Even if you want to be able to edit the document, you might still be able to bind to the document and nodes directly, depending on your needs.
If you need more advanced support (like INotifyPropertyChanged), I suggest that you create a hierarchy of classes like the original API, based on XObject. I would only add properties that I planned on directly supporting in the UI. I would then create a separate model class that could convert from the XDocumentViewModel hierarchy to a real XDocument, and back.
... how [to] represent the Xdocument of an XSD in the ViewModel so that I can select an XNode.
What do you mean by select?
If you mean in the UI, then that's taken care of in the XAML.
If you're talking about querying the view model, then you could try to use the existing LINQ XML querying API against the underlying document. Keep an internal dictionary mapping XObjects back to your view model objects, and when you get results, simply look up each result in the dictionary before returning it.

Binding WPF control to multiple sources (not traditional multibinding)

I am trying to do some databinding magic. I have a Shipments view that lists shipments, and provides filtering and ordering ability on the list. The filter string box, Delivery Status filters (checkboxes) and Ordering Radiobuttons are databound to properties in the ViewModel. I want to add the ability to save state and I have elected to do this by saving control states in an xml document. Previously I have done this before with little problem, using databinding to just read/write the values back and forth.
However, now I have a quandry. My filter controls are currently databound to items in the ViewModel. i can write code that changes their databinding from the xml to the ViewModel on load and vice versa, but that would be messy.
Is there a mechanism in place that I can use to achieve the ability to bind to two equal sources and have them updated at the same time?
This sounds like a concern for the view model.
Why not load the saved values into the view model, and have the view model decide what data to expose?
Then the view doesn't have to be concerned with managing data.
None that I'm aware of.
My opinion: I really wouldn't do this anyway - if your datacontext is the viewmodel, and the viewmodel has properties for the filter, you almost certainly should be persisting and retrieving the relevant viewmodel state to keep the state of the filters. Trying to save controlstate, then retrieve it, set it, and set the viewmodel based on the new controlstate sounds like a lot more work and much more prone to bugs.

Resources