Search code examples
wpfmvvmcaliburn.microavalondock

Avalondock close document with MVVM


We have a working avalondock implementation that listens to onclosing events, if the document is not saved the user gets a chance to save it etc. Works well.

Now a user wants a close button from the File menu and it should work like the built in close button (The little X by the document name).

Only way I have find is not very MVVM friendly.

I databind the CloseCommand to the dockable items ViewModel like

<Setter Property="CloseCommand" Value="{ Binding Model.CloseCommand, Mode=TwoWay}" />

Then from the ViewModel i have a method

public ICommand CloseCommand { get; set; }

public void Close()
{
    if (CloseCommand.CanExecute(this))
    {
        CloseCommand.Execute(this);
    }
}

This works and all the behaviour from pressing the built in close button is retained. But I think its a ugly hack. I'm dependant on that the View databinds the CloseCommand down to the viewmodel etc. There must be a more MVVM way of triggering close?


Solution

  • I solved it like this

    VM

    public ICommand CloseCommand { get; set; }
    
    public void Close()
    {
        if (CloseCommand.CanExecute(this))
        {
            CloseCommand.Execute(this);
        }
    }
    

    View

    <xcad:DockingManager.LayoutItemContainerStyle>
        <Style TargetType="{x:Type xcad:LayoutItem}">
            <Setter Property="Title" Value="{Binding Model.Title}" />
            <Setter Property="IconSource" Value="{Binding Model.Icon}"/>
            <Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
            <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
            <Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter={x:Static Visibility.Hidden}}"/>
            <Setter Property="CloseCommand" Value="{ Binding Model.CloseCommand, Mode=TwoWay}" />
        </Style>
    </xcad:DockingManager.LayoutItemContainerStyle>