0

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?

Anders
  • 17,306
  • 10
  • 76
  • 144
  • Do you want to use `caliburn.micro` or...? – sharp Nov 20 '17 at 23:35
  • Well, I want to sue a more MVVM way. Databind a command on the model and then execute said command from model feels backward – Anders Nov 21 '17 at 14:10
  • While closing, to you call `TryClose()` method from cm? – sharp Nov 21 '17 at 14:22
  • Avalondock does not support that – Anders Nov 21 '17 at 14:30
  • A avalockdock panel inherits PropertyChangeBase, which CM Screen does too. But calling TryClose does nothing – Anders Nov 21 '17 at 14:32
  • When you want to open a new tab/window... do you use `ActivateItem()`? Try overriding `TryClose` and chekc what happens... the method asks Parrent (if it exists, if not it closes app) if there is active child and then it closes it... – sharp Nov 21 '17 at 14:40
  • I just add the viewmodel to a list databound to DokingManager. DocumentsSource – Anders Nov 21 '17 at 14:44
  • Do you have any sample of your situation, to upload the code on git, so I can try? – sharp Nov 21 '17 at 14:47
  • Yeah, sure, the code is open source https://github.com/AndersMalmgren/FreePIE/blob/master/FreePIE.GUI/Views/Main/PanelViewModel.cs#L91 – Anders Nov 21 '17 at 15:03
  • Inside MainShellView.xaml in `DockingManager` add this line `cal:Message.Attach="[Event DocumentClosing]=[Action CloseMe($eventargs)]" ` It should look like this... ` – sharp Nov 22 '17 at 07:34
  • We already use the document closing and document closed events. To use them from code behind is offcourse a option. But you would need to reproduce the cancel code etc that avalon has yourself – Anders Nov 22 '17 at 09:29
  • Well I don't see any other way... I'm always trying to bypass code-behind when using MVVM – sharp Nov 22 '17 at 09:32
  • How do you bind to your Model in the LayoutItem? I cant set the DataConetxt – laserman Sep 22 '21 at 07:56
  • @laserman https://github.com/AndersMalmgren/FreePIE/blob/b7ead60f4bc6fbe492adb111c4dfa542a644dd37/FreePIE.GUI/Shells/MainShellView.xaml#L49 – Anders Sep 22 '21 at 14:12
  • I get: System.Windows.Data Error: 40 : BindingExpression path error: 'DocumentCloseCommand' property not found on 'object' ''LayoutAnchorableItem' (Name='')'. BindingExpression:Path=DocumentCloseCommand; DataItem='LayoutAnchorableItem' (Name=''); target element is 'LayoutAnchorableItem' (Name=''); target property is 'CloseCommand' (type 'ICommand') – laserman Sep 23 '21 at 17:36

1 Answers1

0

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>
Anders
  • 17,306
  • 10
  • 76
  • 144