WPF Prism - list view and data entry dialog - wpf

I have a listview which displays a list of Users (binds to UserListViewModel) - the user can add/edit/delete entries. What I would like to do is have the add button open a new window where the user can enter the new details and save. When the save is successful, I want the window to close and the listview to be refreshed to show the new addition.
What is the best approach for doing this using the MVVM pattern? I've read about using events, modal dialogs, etc, and the accepted answer to this question had a description of what i'm aiming for, but I can't seem to find an example of how this is implemented.
Can someone provide an example?
Update: I ended up finding (read: stumbling across) this article which suits my purposes.

I would go with interactions. This way you will be able to start interaction, pass data to it and then after it's done/closed - get some data back to update your VM.
Download MVVM in a box and they have example of testable MVVM-ish interactions:
http://karlshifflett.wordpress.com/2010/11/07/in-the-box-ndash-mvvm-training/

Related

MVVM without 'standard model'

Almost every tutorial explains MVVM in WPF in the same manner: We have some entity (Person, Student, Car etc) - we create a Model class for it that will only carry data (it might also implement INotifyPropertyChange, IDataErrorInfo and whatnot - doesn't matter now), then we create a View-Model for that class, that will query and/or save our entity/ies somewhere (most likely via Commands) - our View will bind to the View-Model and voilĂ .
Let's say we have an application that won't, for a difference, work with querying/saving entities. Let's say it should contain a button that when clicked will download an .exe file (in a background thread) and will launch it. I'm curious, how would you approach this (architecture-wise) - would you actually create a folder/class named 'Model' for that program? What would it contain? How I see it, I'd have two abstractions - one for downloading a file and one for launching it. I'd inject them to View-Model and call their methods (via Command) and that's it. Or should it be a Model class that contains these abstractions? I know this question might seem strange, but it's something that's been bothering me for some time.
Btw, I realise the title of this post doesn't tell much - I didn't have a better idea for it, if someone does - feel free to change it.
For the example you provide, how the MVVM model will be setup is as follows. Let us assume that you want to download some data and show it in a DataGrid which is part of your view. In the View you add your DataGrid and Button, bind your button click to the ViewModel via a command. Bind your DataGrid via ItemSource to some ObservableCollection<T> in the view model (the view model must implement INotifyPropertyChange). Now, when the user clicks the button the command will fire and you can go and fill you ObservableCollection<T> with data via the download, the downloaded data which is bound to the UI will automatically display...
I hope this helps.

How to put data onto this Silverlight Timeline Control

I've got the following problem: in our teams software you can navigate to a site (journal) which loads a individual history (journal entries). The history entries are currently shown in a grid. If you change the SelectedItem, additional data (details of the journal entry) is shown below the grid.
Now my team finds this Silverlight Timeline Control (Silverlight Documentation) pretty good for displaying historical data. I think this either.
My problem is that the only way to put data onto this timeline control is via XML files. That's not a viable solution for our project. Do you see a way to "bind" this to something like ItemsSource? The reason for this is that we have lots of "journals". And every journal you open shows a different history of journal entries. You also can add/edit/delete entries.
You can do this through ResetEvents method. Timeline control calculates event positions, so it needs all events to calculate position of any. It loads them quickly, though, so 10k of events should not be a problem. Please use timeline forum http://timeline.codeplex.com/discussions for more help.
Yes, add propery which calls reset events. This could be observable collection, subscribe to events of this collection and call reset events from there also. Makes sense?

How can i handle dynamic content in WPF?

I'm trying to realize a chat app using WPF with MVVM approach. I would like to have an interface similar to the last Skype version, with the BuddlyList on the left side and the active chat on the center-right. When the user clicks on a name, the chat panel appears on the right. I want to use DataBinding and I'm asking which is the best UI component to use for handling chat sessions:
A panel with different child panels (all hidden minus the active): maybe simpler to handle but maybe heavy for memory.
A panel that changes content according to the active session (using filters to ObservableCollection): maybe lighter but harder to realize?
Another type of container.
Other solutions.
Can anyone help me with understanding which would be a good way to work with WPF?
As you will have to handle collections of data, like your user list or the list of messages in a chat, you will have to use a control, you can databind the collection to. Therefore you need a control deriving from ItemsControl. There are ListBox or ListView (extended ListBox) or DataGrid for your purpose. Which to go for depends on what you want to do in detail regarding styling, selection of rows, etc.
All of these are suitable in general, so you will just have to do a bit of research to find out, which one is best for what you want to achieve.

MVVM - Closing a tab when a record is deleted

I'm not really sure how to do this in the best MVVM way...
Basically, my main app opens up a search window that shows all records in a TabPanel. Then if a record is double clicked a new tab is opened with that record. Now, I'm trying to keep things MVVM, but I can't for the life of me figure out how to close the gui tabitem when a person deletes the record (why keep it open if the record is gone).
The only way I can figure out how to do it now is to pass the instance of the TabItem as a parameter of the DeleteCommand, which to me seems like a big no-no, but I can't for the life of me figure out how to accomplish this.
If you were going to do this in the true MVVM sense, then double-clicking a record would, behind the scenes, add a record to a collection of records. That record collection is the datasource for the tabs in your control. Simply removing that item from the list (usually an ObservableCollection<T>) would result in the UI updating and the tab being removed.
Which approach are you currently using to show the tab?
Edit (in response to comment):
That is not "true" MVVM. It doesn't matter if the tabs can be more than just records. You should create View Models which abstract those details, then just put your view model instances (RecordViewModel, ReportViewModel, etc) in an observable collection and bind to that. Use datatemplates to render the correct views for each tab's content based on the type of view model the current tab is being bound to.
Josh Smith wrote an amazing article describing how MVVM works. The sample application does something very similar to what you want to do.
The application displays a TabControl which displays 1 or more workspaces. The workspace area displays two different kinds of items. The tab items are closable. Take a look, I'm sure this will solve your problem.
WPF Apps With The Model-View-ViewModel Design Pattern

Managing silverlight RIA services context lifetime

I'm working on a line-of-business silverlight application and I need a piece of advice concerning managing RIA services context lifetime.
Application - afer a few simplifications - is build of one big tab control. At the beginning there are 2 tabs: customer list and invoice list. These are plain datagrids with filtering/sorting and that sort of stuff.
User can add/edit customer or invoice selecting a row and double-clicking. Then the new tab is created with details of customer or invoice. User can open many tabs with different customers/invoices. After editing, user can save and close tab or just abandon edit and close.
The question is how to deal with data contexts.
Should I create one for customerlist and one for invoicelist and when user opens a new tab, I simply bind customer/invoive dataobject to control? This has an advantage that I dont need to refresh grids after saving changes. EDIT: This has some drawbacks. User can refresh grid - and what will happen to open detail tabs? User can also filter grid so some records being edited can be removed from datactx?
The other way is to create datacontext per tabitem. This is more safe but I need to handle refreshing grid(s).
I have no idea which method is better or maybe there is another alternative?
Use one ObservableCollection list in each case and it will automatically update the datagrids when items are changed.

Resources