6

I'm trying to have a MainWindow that is bound to the a view. I change that view in code and expect it to update in the Main Window, however that is not happening.

I have this code in my XAML

<Grid>
    <ContentControl Content="{Binding Source={StaticResource ViewModelLocator}, Path=MainWindowViewModel.CurrentControl}" />
</Grid>

I then change my Control via this code

public class MainWindowViewModel : ReactiveObject
{
    private UserControl _CurrentControl = null;
    public UserControl CurrentControl
    {
        get
        {
            if (_CurrentControl == null)
            {
                _CurrentControl = new HomePage();
            }
            return _CurrentControl;
        }
        set
        {
            this.RaiseAndSetIfChanged(x => x.CurrentControl, value);
        }
    }
}

As you can see I'm using the ReactiveUI library.

Is ContentControl the wrong thing to use in that view or am I just not binding and updating correctly?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Rumel
  • 917
  • 1
  • 9
  • 21
  • does RaiseAndSetIfChanged implement INotifyPropertyCHanged? If not then I would add it so that the UI is made aware of the changes – J King Mar 12 '13 at 05:43
  • @JKing I believe it does, it's from the ReactiveUI library – Rumel Mar 12 '13 at 05:45
  • 1
    I don't know that library, do you have access to its source code to confirm. Other wise you could implement it yourself in the CurrentControl setter and see if it solves your problem. I have had other problems with the UI not updating and it usually turned out to be a problem with INotifyPropertyChanged – J King Mar 12 '13 at 05:50
  • can you post more of the xaml – J King Mar 12 '13 at 05:51
  • You most certainly can databind to the `Content` property. Look for binding errors. – Nikita B Mar 12 '13 at 05:22
  • The current binding is correct as it shows the first view, it just doesn't update to a new view. – Rumel Mar 12 '13 at 05:37
  • @Rumel: Imho, you should a) check if the _CurrentControl value actually changes after you call the property setter the second time b) try to implement INotifyPropertyChanged yourself (wont take more than a minute) and see if that helps – Nikita B Mar 12 '13 at 05:50
  • @Nik `ReactiveObject` already Implements `INotifyPropertyChanged` Refer: [Using ReactiveUI to Integrate INotifyPropertyChanged & IObservable](http://www.danharman.net/2011/06/13/using-reactiveui-to-integrate-inotifypropertychanged-iobservable/) – Harsh Baid Mar 12 '13 at 06:16

3 Answers3

11

There is actually a far better way to do this, using ViewModelViewHost:

<Grid DataContext="{Binding ViewModel, ElementName=TheUserControl}">
    <ViewModelViewHost ViewModel="{Binding CurrentControlViewModel}" />
</Grid>

Now, your class will look something like:

public class MainWindowViewModel : ReactiveObject
{
    private ReactiveObject _CurrentControlViewModel = new HomePageViewModel();
    public ReactiveObject CurrentControlViewModel {
        get { return _CurrentControl; }
        set { this.RaiseAndSetIfChanged(x => x.CurrentControlViewModel, value); }
    }
}

And somewhere in your app's startup, you should write:

RxApp.Register(typeof(IViewFor<HomePageViewModel>), typeof(HomePage));

What's ViewModelViewHost?

ViewModelViewHost will take a ViewModel object that you provide via Bindings, and look up a View that fits it, using Service Location. The Register call is how you can associate Views with ViewModels.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
5

why you call your class MainWindowViewModel? when you wanna do mvvm you shouldn't have properties with type UserControl in your VM.

the usual mvvm way looks like this:

  • viewmodel with INotifyPropertyChanged
public class MyViewmodel
{
    public IWorkspace MyContent {get;set;}
}
  • xaml content control with binding to your VM
<ContentControl Content="{Binding MyContent}"/>
  • datatemplate --> so that wpf knows how to render your IWorkspace
<DataTemplate DataType="{x:Type local:MyIWorkSpaceImplementationType}" >
   <view:MyWorkspaceView />
</DataTemplate>
blindmeis
  • 22,175
  • 7
  • 55
  • 74
3

I think you have several muddled concepts here and they are getting in each others way.

Firstly you aren't actually using ANY of the reactiveUI code, it never gets called. Since your get accessor implements a lazy instantiation pattern then it means the set accessor is ignored. This means that the view never notifies the property change, so you never get updates.

I'd recommend using something more like

private UserControl _currentControl;

public MainWindowVirwModel()
{
  CurrentControl = new HomePage();
}

public UserControl CurrentControl
{
  get { return _curentControl;}
  set { this.RaiseAndSetIfChanged(...); }
}

In addition, this still mixes up View components i.e. HomePage, inside your ViewModel tier which will making unit testing far more difficult.

AlSki
  • 6,868
  • 1
  • 26
  • 39