0

Is it correct to change model instances in runtime? My control was bound to first instance, but during the program execution I d like to bind them to another instance.

somewheere in ViewModel class :

//ViewDefault - already initialized
// View - will be ready later

public string TextProperty
{
get
            {
                if (View != null)
                {
                    return View.Model.text;
                } return ViewDefault.Model.text;
            }
            set
            {
                 if(View != null)
                   {
                   //.. logic with View.Model.text
                   }else{
                      // logic with ViewDefault.Model.text
                   }
                   RaiseOnPropertyChanged("TextProperty");
}

The question is - what I must do to notify my View that a binding content is changed?

<Setter Property="Text" Value="{Binding MyViewModel.TextProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

*ViewDefault.Model and View.Model have one type

curiousity
  • 4,703
  • 8
  • 39
  • 59

1 Answers1

0

In my point of view a ViewModel links a View with a Model so if I had to change the Model I would create a new ViewModel and attach it to the View's DataContext .

The view should be created by someone (a factory preferably) and the one creating the view should be creating the datacontext too and attaching it doing

View view = new View();
view.DataContext = new ViewModel();
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • How to attach View's DataContext at runtime(from source)? It was pre-defined in xaml beforehand – curiousity Jun 30 '15 at 09:49
  • This is almost always a bad decision. Please check my updated answer – Ignacio Soler Garcia Jun 30 '15 at 10:07
  • I'd [disagree](http://stackoverflow.com/q/13085670/1997232): viewmodel primarily services *view*. Single ViewModel can handle multiple Models. The idea to have multiple ViewModels per view *smells* badly as for me. View uses binding to bind to a property in ViewModel. What property returns (ModelA property or ModelB property) - it doesn't matter *for the view*. Also, there is nothing bad to bind [DataSource in xaml](http://stackoverflow.com/a/4590558/1997232). Those two reasons and very *abstract* nature of the answer makes me want to downvote the answer. Shall I? – Sinatr Jun 30 '15 at 10:24
  • You can recalculate all the viewmodel properties when changing the model but seems easier just to create a new viewmodel. Changing it will refresh all the bindings so that's find. On the other hand adding logic to the xaml is a bad idea imho as you are adding logic (the viewmodel creation) to a view. This has several problems, you are breaking boundaries, mixing responsibilities, you cannot test nor debug the viewmodel creationg, etc. – Ignacio Soler Garcia Jun 30 '15 at 12:50
  • Having said that I've refreshed the property values several times in the past but the other solution is simpler in most situations. – Ignacio Soler Garcia Jun 30 '15 at 12:50