Pregunta

I have a MainView that holds a single ContentControl, the ContentControl loads up a default usercontrol when the application loads.

<ContentControl x:Name="MainContentArea" Content="{Binding ActiveControl}"/>

The usercontrol that is loaded, loads a few plugins (irrelevant) and upon selecting an item from a combobox, it triggers a ICommand that exists in the Parent(MainViewModel) using MVVM Light´s concept of ViewModelLocator.

private void CreateSelectedPlugin(IExtendedUIViewFactory plugin)
    {
        var pluginStartControl = plugin.Create();
        _locator.Main.DefaultCommand.Execute(pluginStartControl);
    }

The problem is that the ContentControl is not updated, i can set a breakpoint and see that the command is executed in the MainViewModel and that the variable i send is valid.

public ICommand DefaultCommand { get; set; }
    public MainWindowViewModel()
    {
        DefaultCommand = new RelayCommand<object>(LoadSection, o => true);
    }

    private void LoadSection(object plugin)
    {
        ActiveControl = plugin;
        //does not matter if i set it to null here
    }

Calling the LoadSection or testfunction which just sets the ContentControl to null, from the MainView/MainViewModel, it works as expected.

What hold does the command i send from within the control have on the Contentcontrol that makes it not want to load something else?

¿Fue útil?

Solución

you need to notify the UI that there has been a change. Implemement INotifyPropertyChanged and add it to the the ActiveControl assignment:

private void LoadSection(object plugin)
{
    ActiveControl = plugin;
    NotifyPropertyChanged();
}

Here is the documentation

EDIT#1

I think you should use the button in your user control to bind to the command in your main window. This is better than trying to pass the mainwindow viewmodel to the usercontrol, which creates a dependency and violates the MVVM pattern. Add this button to your usercontrol1 in the sample you gave me

        <Button Content="Set MyContent to null using binding to main window command" Height="40" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.PublicCommand}"></Button>

It uses a relative binding to the command in the main window, which in my app, I use one main window to hold all usercontrols/views. That way I can control what is showing and use command definitions only in one place that I know will always be available.

hope this helps

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top