Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use data binding with the Content property of a WPF Frame?

I can use data binding to set the initial Content of a WPF Frame, but subsequent changes to the the bound property (implemented using INotifyPropertyChange) do not seem to change the content.

Also, does anyone know if binding directly to the Content property in this way will cause the bound item to appear in the Frame or NavigationWindow's journal?

Some context: I realize that I should probably be using the NavigationService to interact with the Frame, but I'm attempting to follow the MVVM pattern. It seems like it would be much simpler to databind to the Content property...

like image 442
dthrasher Avatar asked Apr 23 '10 17:04

dthrasher


2 Answers

You can use data binding against a Frame, but you need to make sure the Mode for your Binding is set to TwoWay.

XAML:

<Frame Content={Binding Path=MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} />

View Model:

public class MyViewModel : INotifyPropertyChanging, INotifyPropertyChanged
{
  public Page MyProperty
  {
    get
    {
      return _viewModelPage;
    }

    set
    {
      this.OnPropertyChanging("MyProperty");
      _viewModelPage = value;
      this.OnPropertyChanged("MyProperty");
    }
  }
}
like image 91
Oppositional Avatar answered Nov 17 '22 20:11

Oppositional


Many in the WPF community agree that the built-in navigation framework is broken. However, even if you were to use it, binding the Content property is not the correct approach. If you want to use MVVM with navigation you should combine it with the FrontController pattern where the ViewModel dispatches a navigation request to a Controller which then resolves that request for you. There aren't many examples of this concept available because (as I mentioned before) many developers pass on using WPF's built-in navigation.

If you want to look at a very robust navigation engine for WPF, look at nRoute It is a port of the MVC routing engine to WPF.

like image 38
Michael Brown Avatar answered Nov 17 '22 21:11

Michael Brown