Heinrich
Heinrich

Reputation: 2214

Getting to grips with MVVM with WPF

I just got some design/implementation questions for MVVM with wpf, c#. I'm just trying to get to grips with MVVM and was wandering if someone could confirm my ideas, in my application I need to a employee, a department and a company set up. So in other words I have a employee model, and a corresponding employee ViewModel. Now when the user clicks New Employee brings up a window that has 4 things 2 combo boxes and and employee name field and a submit button. Now my main focus is on how do I manage the 2 combo boxes, one being for company and one for department?

Now I also have two models for company and department and viewmodels, respectively, because I need to be able to add new a company or department.

So my first question boils down to the fact that do I used the company and department models in the Employee Viewmodel, or do I bind the drop down boxes to the separate viewmodel for company or department? If so how would i get the combo box details then, if done the second way?

And my second is if you bind a datagrid to a, lets say, EmployeeListViewModel, if any changes occur in the list in employeelistviewmodel, does that datagrid automatically update or do I have to call a function that rechecks the list or do i have to rebind it??

Any help would be great :)

Thanks all

Upvotes: 1

Views: 398

Answers (3)

Robert Rossney
Robert Rossney

Reputation: 96702

Don't think of the employee view model as a model of the employee. Think of it as a model of the employee view. So, everything that's going to appear in the employee view needs to be present in the view model - and that includes whatever is needed to populate the combo boxes in that view - so there will be a Companies property and a Departments property that those combo boxes' ItemSource properties will bind to.

You may have view models of the company and department, which will support the views used to display/modify those objects. This is usually a completely different problem than displaying the descriptions of those objects in combo box items. (Not always, though - for instance, if the combo box items need to be updated when the items they're bound to changes, you'll need them to be bound to view models that support property-change notification.)

As far as your second question goes, if you have an ItemsControl whose ItemsSource is bound to a collection of view model objects, as long as those view models support change notification (which is one of the main reasons to implement a view model), and are stored in a collection that supports change notification (like an observable collection), keeping the ItemsControl synchronized happens automatically.

Upvotes: 4

John Petrak
John Petrak

Reputation: 2928

I would put the Company and Department data in the employee ViewModel. Using Company as an example I would have two properties for it

ObservableCollection<CompanyViewModel> ListOfCompanies;
CompanyViewModel SelectedCompany;

ListOfCompanies is bound to the itemsource of the combobox and the SelectedCompany is bound (twoway) to the selectedItem of the combobox. That way, you can set selected company in the viewmodel and it will updated the UI and the viewmodel will also be updated by the UI when it changes.

Depending on how you populate the ListOfCompanies and SelectedCompany, you may need the CompanyViewModel to implement the following interfaces.

IComparable<CompanyViewModel>, IEquatable<CompanyViewModel> 

The binding will do the rest.

As for automatically updating your employee datagrid, I prefer to use a ListView myself, but an observable collection of employeeViewModels should refresh fine.

Thats my two bits worth.

Upvotes: 0

aqwert
aqwert

Reputation: 10789

Your view that has the 2 comboboxes will need to expose a collection of company and department items. These could be just models but better if it is wrapped inside a view model. I assume when you select a company the department list will be updated. You can look at a solution I suggested for Cascading combo boxes which update a child list based on a selection of its parent

At a minimum you have a list of items for the ComboBox to bind to and a property for the selected item. I use an 'ItemListViewModel' to group those together.

class MyViewModel : INotifyPropertyChanged
{
  public MyViewModel()
  {
    Companies = new ItemListViewModel<string>();
    Departments = new ItemListViewModel<string>();
    ...
 }

  public ItemListViewModel<string> Companies { get; set; }
  public ItemListViewModel<string> Departments { get; set; }
}

In XAML

<ComboBox ItemsSource="{Binding Companies}" />
<ConboBox ItemsSource="{Binding Departments}" />

As far as getting started with the basics there is plenty of other posts around that will get to this point but for the ComboBox design the above should help.

For your second question as long as your ViewModel implements INotifyPropertyChanged it will update the controls that object is bound to.

Upvotes: 1

Related Questions