0

Hoping someone could clear things up. In the following ViewModel, does using Entity Framework as my model eliminate the need to use [Model] and [[ViewModelToModel(...)] attributes? The code runs the same with or without them, because the binding in the view ignores them and binds to the ObservableCollection.

Comments?

 public class MainWindowViewModel : ViewModelBase
{
    Models.OneHour_DataEntities ctx;

    public MainWindowViewModel()
        : base()
    {
        Save = new Command(OnSaveExecute, OnSaveCanExecute);

        ctx = new Models.OneHour_DataEntities();
        Customers = new ObservableCollection<Models.Customer>(ctx.Customers);
    }

    public ObservableCollection<Models.Customer> Customers
    {
        get { return GetValue<ObservableCollection<Models.Customer>>(CustomersProperty); }
        set { SetValue(CustomersProperty, value); }
    }

    public static readonly PropertyData CustomersProperty = RegisterProperty("Customers", typeof(ObservableCollection<Models.Customer>), null);

    public Command Save { get; private set; }
     private bool OnSaveCanExecute()
     {
         return true;
     }

     private void OnSaveExecute()
    {
        ctx.SaveChanges();
    }

}

1 Answers1

0

Catel uses different interfaces to take advantage of the models. For example, it uses the following interfaces:

  • IEditableObject => undoing changes to model when user cancels
  • INotifyPropertyChanged => update view model when model updates

If your entity model implements these interfaces, you can define a property as a model.

In your example however, you use an ObservableCollection (thus a list of models) as a model. That is not supported (or, again, the collection must support IEditableObject and INotifyPropertyChanged).

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32
  • I've found an answer to this by modifying the template in the Model.tt file. I can't locate it right now but the post shouldn't be too hard to find. – Johnny Beatniks Jul 06 '13 at 02:58