0

I am new to MVVM and am writting a little test-application using it. I've got a Model, which represents the data structure - a ViewModel and a View (parent class is Page), too.

Now i would like to pass some initial data to the Model, so the window could show me these.

In a sample application of David Anderson, he passes the data as method-parameter, which is actually the right way and causes not the trigger of the PropertyChanged-Event, but my Model-class is quite "fat" - it gots a lot of properties (> 30). So, how shall i realize it in this case? I don't think a method with more then 30 parameters is the right way to handle that. Or am i wrong?

Does someone has an idea, how professionals realize this?

Here is my used code:

View (PersonPropertiesView is a subclass of the Page-class)

XAML

<TextBlock Text="{Binding Person.ID, UpdateSourceTrigger=PropertyChanged}" />

Code-Behind

public PersonPropertiesView()
{
    InitializeComponent();
    this.DataContext = new PersonPropertiesViewModel();
}

ViewModel (PersonPropertiesViewModel)

Code

private Person _Person;

public Person Person
{
    get
    {
        return this._Person;
    }
}

public Person()
{
    this._Person = new Individual();
    this._Person.ID = 12;
}

Model (Person, inherits INotifyPropertyChanged)

private long _ID;

public long ID
{
    get
    {
        return this._ID;
    }
    set
    {
       if (this._ID != value)
       {
           this._ID = value;
           OnPropertyChanged("ID");
       }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

If i try to compile this code, i get the System.Reflection.TargetInvocationException exception. Does someone knows why?

  • This might help you out: http://stackoverflow.com/questions/40264/how-many-constructor-arguments-is-too-many?rq=1 – Chris Jan 12 '14 at 00:17

1 Answers1

0

My first reaction would be: why? I'm not sure this is too important - when setting these properties immediately after construction, I would imagine it's fairly likely that this occcurs before the ViewModel is bound to any View (though without the context, I couldn't be sure).

The cost of attempting to fire the PropertyChanged event with no observers is likely too small to measure.

In the code you've posted, this code will throw an exception when there are no listeners:

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

You need to check if the event is null, not the property name:

protected void OnPropertyChanged(string propertyName)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Hey Charles, if i do so, i get an exception of the type "System.Reflection.TargetInvocationException". I will post my code, mayby someone will find the mistake. – Martin Sheppard Jan 12 '14 at 16:56