Prevent document from closing in DockingManager

南楼画角 提交于 2019-12-11 10:29:20

问题


Here's a sample, which uses DockingManager (a.k.a AvalonDock) from Extended WPF Toolkit.

View model:

public class Person
{
    public string Name { get; set; }
    public bool CanClose { get; set; }
}

View:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
        xmlns:local="clr-namespace:WpfApplication2">
    <Grid>
        <xcad:DockingManager DocumentsSource="{Binding}">
            <xcad:DockingManager.Resources>
                <DataTemplate DataType="{x:Type local:Person}">
                    <StackPanel>
                        <TextBlock Text="Here's person name:"/>
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </xcad:DockingManager.Resources>

            <xcad:DockingManager.DocumentHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Content.Name}" />
                </DataTemplate>
            </xcad:DockingManager.DocumentHeaderTemplate>

            <xcad:LayoutRoot>
                <xcad:LayoutPanel Orientation="Horizontal">
                    <xcad:LayoutDocumentPane />
                </xcad:LayoutPanel>
            </xcad:LayoutRoot>
        </xcad:DockingManager>
    </Grid>
</Window>

Code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new[]
        {
            new Person { Name = "John" },
            new Person { Name = "Mary", CanClose = true },
            new Person { Name = "Peter", CanClose = true },
            new Person { Name = "Sarah", CanClose = true },
        };
    }
}

I want to prevent documents from closing via CanClose property in my view model. I've expected, that there must be some style for documents container, so, I'll write something like:

<Setter Property="CanClose" Value="{Binding Content.CanClose}"/>

and everything will work. But looks like there's no such style in DockingManager.

Am I missing something?

Update.

Of course, I can write an attached behavior, which will listen to DockingManager.DocumentClosing event and dispatch it to any view model, which will be bound to DockingManager. But it seems to me very stupid...

Another way is to handle event in the view:

private void DockingManager_DocumentClosing(object sender, Xceed.Wpf.AvalonDock.DocumentClosingEventArgs e)
{
    e.Cancel = !((Person)e.Document.Content).CanClose;
}

But it is definitely not a MVVM-way, and I like data binding.


回答1:


If you have a ContentViewModel - you can have an ICommand Close {get;set;} property and bind it to the LayoutItem's close command.

You can use a DelegateCommand for this on your ContentViewModel which can be used to determine if you can close the document or not (setting e.Cancel = true should stop the close command).



来源:https://stackoverflow.com/questions/17185780/prevent-document-from-closing-in-dockingmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!