Binding WPF Data to an External Data Model

I recently started developing an application using WPF and I am really silent about the following:

I have a domain model of my application which is simple POCO objects serialized to / from hard drive. Then I have a WPF application and I would like to bind it to different parts of the model. I need to be able to notify the UI of base model changes (for example, implement INotifyPropertyChanged), but I want to do this without interconnecting with my model (read without changing the current model implementation). How can I implement change notification other than model change? The reason I want to do this is because I am sharing the model across multiple projects, only one of them is WPF and I don't want to add extra code to the model. One thing that came to my mind was to create a "copy" of the model (with INotifyPropertyChanges and BindingLists, etc.)but it seems to be difficult to maintain ... Thanks in advance.

Ondrej

+1


a source to share


2 answers


I see two possible solutions here:



  • Use only seperate model for WPF screens only ( MVVM pattern ). This would require supporting two different models, and preparing for a lot of display code.
  • Use PostSharp to "enhance" your model with all the boilerplate code you need. Here you can find an example of automatic implementation of INotifyPropertyChanged. Remember that introducing PostSharp into a project is an important decision, so I suggest that you familiarize yourself with it first.
0


a source


Check out MVVM

Download the source code to the hierarchy.



Basically, you still keep simple POCO objects as your models. Then you create a ViewModel around the model like this:

public class CustomerViewModel : INotifyPropertyChanged
{
        readonly Customer _customer;
        public CustomerViewModel(Customer customer)
        {
            _customer = customer;
        }

        public string FirstName
        {
            get { return _customer.FirstName; }
            set
            {
                if (value == _customer.FirstName)
                    return;

                _customer.FirstName = value;

                OnPropertyChanged("FirstName");
            }
        }
        ...
}

      

+2


a source







All Articles