从DockingManager关闭防止文件 [英] Prevent document from closing in DockingManager
问题描述
下面是一个示例,它使用 DockingManager
(又名 AvalonDock )从扩展WPF工具包
Here's a sample, which uses DockingManager
(a.k.a AvalonDock) from Extended WPF Toolkit.
查看模式:
public class Person
{
public string Name { get; set; }
public bool CanClose { get; set; }
}
查看:
<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 },
};
}
}
我想阻止文件的通过<$ C关闭$ C> CanClose 在我看来模型属性。
我预期的那样,必须有一些风格文件的容器,所以,我会写这样的:
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}"/>
和一切都将正常工作。不过貌似有一个在 DockingManager
没有这样的风格。
and everything will work. But looks like there's no such style in DockingManager
.
我缺少的东西吗?
更新
当然,我可以写一个附加的行为,这会听 DockingManager.DocumentClosing
事件并将其分派到任何视图模式,这将绑定到 DockingManager
。但在我看来很愚蠢......
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;
}
但它绝对不是一个MVVM路,我喜欢数据绑定。
But it is definitely not a MVVM-way, and I like data binding.
推荐答案
如果你有一个ContentViewModel - 你可以有一个ICommand关闭{获取;集;}属性,并将其绑定到LayoutItem的关闭命令。
If you have a ContentViewModel - you can have an ICommand Close {get;set;} property and bind it to the LayoutItem's close command.
您可以使用DelegateCommand这个你ContentViewModel它可以被用来确定是否可以关闭该文件或不(设置e.Cancel =真应该停止关闭命令)。
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).
这篇关于从DockingManager关闭防止文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!