Vojtech B
Vojtech B

Reputation: 2967

Caliburn.Micro assign resolved ViewModel instance to child from parent

controls:TabControlView is a reusable component. I would need to be able to create a ViewModel/retrieve its instance for each component usage.

<UserControl x:Class="App.Views.Shell.ShellView" ...>
    <StackPanel Orientation="Vertical">

        <controls:TabControlView cal:Bind.Model="{Binding TabControlViewModel}"/>

    </StackPanel>
</UserControl>

In ShellViewModel constructor:

public TabControlViewModel TabControlViewModel { get; set; }
public ShellViewModel(){
    TabControlViewModel = new TabControlViewModel();//For simplicity. It is resolved by IoC
}

When I put a break point into TabControlViewModel's constructor I can see that it is being called 2 times.

When I setup IoC to resolve TabControlViewModel as singleton it works (as the internal call for resolving TabControlViewModel is served the same instance).

How should I edit my code so it doesn't call BootstrapperBase.GetInstance() automatically or how can I replace the View's ViewModel?

Upvotes: 2

Views: 253

Answers (1)

Vojtech B
Vojtech B

Reputation: 2967

I found out that as the ViewModel is resolved by Caliburn it is then automatically injected to property:

public TabControlViewModel TabControlViewModel { get; set; }

All I needed to do is to make it propfull and do setup of the handed instance there:

private TabControlViewModel _tabControlViewModel;

public TabControlViewModel TabControlViewModel
{
    get { return _tabControlViewModel; }
    set
    {
        _tabControlViewModel = value;
        //Init here
        NotifyOfPropertyChange(() => TabControlViewModel);
    }
}

Upvotes: 1

Related Questions