Ad

A Simpel WPS Using MVVM And EF

- 1 answer

I'm on the edge of going nuts.

I have a working WinForms/database application, that I try to rebuild with WPF. My main problem is that I haven't worked with MVVM before and I can't seem to get the grip of the databinding from Entity Framework to the View.

My WinForms is build exclusively using code-behind, which I know is bad praxis, but it works. I have read about 100 articles, tutorials and examples, downloaded a couples of demo/samples using MVVM. Including "WPF Application Framework (WAF)"

But I haven't found a simple solution/sample how to use EF as the Model or as a dataprovider for the Model. And to pass the information on to to the ViewModel, and lastly bind it from the View.

All the tutorials I have read only describe fetching data from a static list, I need the usual CRUD operations on the database.

I know questions like this are being asked all the time here, but I haven't being able to find an answer to pushing data from EF to the View(Model) and update back through EF. I hope that some of you can help a (must be retarded) person like me with some guiding.

Ad

Answer

The MVVM pattern hinges, a lot, on the INotifyPropertyChanged interface which EF implements so some of the work here is done for you.

I am not too sure on your particular requirement but start off by creating a view mode for your window. Lets assume that you have a entity called Person with an attribute of FirstName as a string and an Id of Int32 (of course), and that all the work for the data layer is taken care of. I will show you how to list the Person entity and allow edit of the person entity as well.

ViewModel

public class Window1ViewModel : INotifyPropertyChanged {
  public event PropertyChangedEventHandler PropertyChanged;

  private ObservableCollection<Person> _people;
  private Person _selectedPerson;

  public ObservableCollection<Person> People {
    get { return _people; }
    set { _people = value;
      this.RaisePropertyChanged("People");
    }
  }

  public Person SelectedPerson {
    get { return _selectedPerson; }
    set { _selectedPerson = value;
      this.RaisePropertyChanged("SelectedPerson");
  }

  public Window1ViewModel() {
    // Instead of setting to empty collection populate with data from EF.
    this.People = new ObservableCollection<Person>();
  }

  private void RaisePropertyChanged(string propertyName) {
    if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
}

Now that you have your ViewModel taken care of it is time to create the UI and bind it.

Binding ListBox

<ListBox ItemsSource="{Binding Source={StaticResource viewModel}, Path=People}" SelectedItem="{Binding Source={StaticResource viewModel}, Path=SelectedPerson, Mode=TwoWay}"/>

Binding TextBox

<TextBox Text="{Binding Source={StaticResource viewModel}, Path=SelectedPerson.FirstName, Mode=TwoWay}"/>

Lastly you could create a button on your Window, bind it to an ICommand on your ViewModel to call the Save() method in the EF layer.

There is a lot you could write to meet your requirement i hope this goes some way to assist. Also recommend the following information. http://www.codeproject.com/Articles/81484/A-Practical-Quick-start-Tutorial-on-MVVM-in-WPF

Ad
source: stackoverflow.com
Ad