2013-06-19 102 views
1

下面是使用Extended WPF Toolkit的DockingManager(又名AvalonDock)的示例。防止文档在DockingManager中关闭

视图模型:

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> 

代码隐藏:

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 }, 
     }; 
    } 
} 

我想阻止文档经由CanClose财产在我的视图模型关闭。 我所料,那一定是有风格的文档容器,所以,我会写这样的:

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

,一切都将正常工作。但看起来像DockingManager没有这样的风格。

我错过了什么吗?

更新

当然,我可以编写一个附加行为,它将听取DockingManager.DocumentClosing事件并将其分派到任何视图模型,该视图模型将绑定到DockingManager。但在我看来很愚蠢......

另一种方式是在视图来处理事件:

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

但它绝对不是一个MVVM路,我喜欢数据绑定。

+0

*耸耸肩*对不起。 –

回答

1

如果您有ContentViewModel - 您可以拥有ICommand Close {get; set;}属性并将其绑定到LayoutItem的close命令。

您可以在您的ContentViewModel上使用DelegateCommand来确定是否可以关闭文档(设置e.Cancel = true应该停止关闭命令)。