0

I have a content control:

<ContentControl Content="{Binding PageViewModel}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="ContentChanged">
            <i:InvokeCommandAction Command="{Binding MyCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ContentControl>

I need to invoke MyCommand every time PageViewModel changes.

However the command is not getting triggered.

Is this possible to do?

mm8
  • 163,881
  • 10
  • 57
  • 88
John
  • 263
  • 1
  • 15

1 Answers1

1

You should handle this in the setter of the PageViewModel property:

private object pageViewModel;
public object PageViewModel
{
    get { return pageViewModel; }
    set
    {
        pageViewModel = value;
        OnPropertyChanged();
        MyCommand.Execute(null);
    }
}

A ContentControl has no ContentChanged event. It has a protected OnContentChanged method that you could override if you create a custom ControlControl class though:

WPF: How to created a routed event for content changed?

You can create a custom event that you raise in this method.

But the MVVM way of doing this would be to invoke the command in the view model when the PageViewModel property is set.

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88