benjgorman
benjgorman

Reputation: 722

Binding of a ContentControl from DataTemplate only working in constructor?

I'm trying to switch the content of a content control through using ICommands. Now setting this property works in the constructor but not in any of the commands.

I have this in my app.xaml

<Application.Resources>
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
    <DataTemplate DataType="{x:Type vm:HomeViewModel}">
        <views:HomeView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:DeviceViewModel}">
        <views:DeviceView />
    </DataTemplate>
</Application.Resources>

This is a snippet from the ShellView.xaml (Which is the view which contains the content control I wish to change):

<ContentControl Content="{Binding Path=CurrentViewModel}" />

Another snippet here showing the button binding:

<Button Content="Button"
                Height="23"
                Name="button2"
                Width="75"
                Command="{Binding Path=DeviceViewCommand}" />

Here is the constructor from the ShellViewModel. As I said setting the CurrentViewModel works here. (You'll notice I set device, then home as a test.)

    public ShellViewModel()
    {
        CurrentViewModel = ShellViewModel._deviceViewModel;
        CurrentViewModel = ShellViewModel._homeViewModel;
        HomeViewCommand = new RelayCommand(() => ExecuteHomeViewCommand());
        DeviceViewCommand = new RelayCommand(() => ExecuteDeviceViewCommand());
        LogOut = new RelayCommand(() => LogOutExecute(), () => true);

    }

    private void ExecuteDeviceViewCommand()
    {
        CurrentViewModel = ShellViewModel._deviceViewModel;

    }

Am I doing something wrong here?

This is also the property changed for the current view model. Should have added this earlier.

public ViewModelBase CurrentViewModel
    {
        get
        {
            return _currentViewModel;
        }
        set
        {
            if (_currentViewModel == value)
                return;
            _currentViewModel = value;
            RaisePropertyChanged("CurrentViewModel");
        }
    }

Upvotes: 0

Views: 684

Answers (3)

benjgorman
benjgorman

Reputation: 722

So I had to solve this by breaking the MVVM pattern. I used the MVVM light messenger class in the code behind of my shell view model, to simply set the content control to a new view and then set it's data context to the ShellViewModel's current view model.

I'm not completely happy with this solution, but it does function correctly.

Upvotes: 0

Silvermind
Silvermind

Reputation: 5944

I would take a step back and give the ContentControl a name and try to set the Content Property directly to see if something else is wrong. Furthermore, how is the DataContext set for the ContentControl? Try to set breakpoint's on setters. You could also check the output window for errors on Bindings.

Upvotes: 1

dvvrd
dvvrd

Reputation: 1689

If I correctly understood your probled, you can create VM class for CurrentViewModel, inherit it from INotifyPropertyChanged and modify it`s property. Bindng Should be one-way.

Upvotes: 1

Related Questions