user_pv
user_pv

Reputation: 5

Detach MVVM View in Catel

I have a Catel.Window as MainWindow with a menu, and the main content is:

 <ContentControl Content="{Binding ActualVM, Converter={catel:ViewModelToViewConverter}}" />

In the MainWindowViewModel:

public ViewModelBase ActualVM { get; set; }

The MainWindowViewModel sets ActualVM in the menu command's OnExecute methods. It works fine. I want to be able to detach the actual view to a new window. In one view's code behind I put the following for a button's click:

    protected void DetachClick(object obj, RoutedEventArgs e)
    {
        ContentPresenter vp = this.GetVisualParent() as ContentPresenter;
        if (vp != null)
        {
            vp.Content = null;
            var dw = new DetachWindow();
            dw.Content = this;
            dw.Show();
        }
    }

DetachWindow is an "empty" Catel.Window with an "empty" ViewModel. It also works fine, the view and viewmodell are functioning in the detached window, but if I click on one of the MainWindows's menuitems, the MainWindowViewModel sets the ActualVM, but the MainWindows does not shows the view as it did before the detach.

Upvotes: 0

Views: 71

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

The reason for this is that you are killing the binding when using this code:

vp.Content = null;

You should set the value on the VM to null so the binding gets updated correctly instead of replacing the binding with a new value.

Another option your could try is to use SetCurrentValue instead of .Content = null.

Upvotes: 1

Related Questions