2013-08-21 178 views
5

我在WPF項目中使用MVVM的AvalonDock。WPF - AvalonDock - 關閉文檔

當我點擊「X」(選項卡的關閉按鈕)時,我的文檔關閉但仍保留在內存中。它似乎只是隱藏起來的。它不會從我的Model.Documents集合中刪除。

如果我添加DockingManager_DocumentClosing並嘗試從集合中刪除我的文檔,我會收到以下方法Xceed.Wpf.AvalonDock.Layout.LayoutContent中的異常,因爲parentAsContainer爲空。

​​

有誰知道我怎麼能管理AvalonDock文檔,可以從我的Model.Documents爲了除去被最終處置時,我打了Close按鈕?

供參考:這是我的AvalonDock的XAML:

<avalonDock:DockingManager 
    x:Name="DockingManager" 
    DocumentsSource="{Binding DocumentItems}" 
    ActiveContent="{Binding ActiveMainWindowViewModel, 
     Converter={StaticResource RestrictedClassConverter}, 
     ConverterParameter={x:Type multiSimAnalysis:MainWindowViewModel}, 
     Mode=TwoWay}" 
    DocumentClosing="DockingManager_DocumentClosing" 
    ActiveContentChanged="DockingManager_ActiveContentChanged"> 

    <avalonDock:DockingManager.LayoutItemContainerStyleSelector> 
    <pane:PanesStyleSelector> 
     <pane:PanesStyleSelector.MainWindowViewLcStyle> 
     <Style TargetType="{x:Type avalonDock:LayoutItem}"> 
      <Setter Property="Title" Value="{Binding Model.Title}"/> 
      <Setter Property="ToolTip" Value="{Binding Model.Title}"/> 
      <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}"/> 
      <Setter Property="IconSource" Value="{Binding Model.IconSource}"/> 
      <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/> 
      <Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/> 
      <Setter Property="ContentId" Value="{Binding Model.ContentId}"/> 
     </Style> 
     </pane:PanesStyleSelector.MainWindowViewLcStyle> 
    </pane:PanesStyleSelector> 
    </avalonDock:DockingManager.LayoutItemContainerStyleSelector> 

    <avalonDock:DockingManager.LayoutItemTemplateSelector> 
    <multiSimAnalysis:PanesTemplateSelector> 
     <multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate> 
     <DataTemplate> 
      <multiSimAnalysis:MainWindowViewLc /> 
     </DataTemplate> 
     </multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate> 
    </multiSimAnalysis:PanesTemplateSelector> 
    </avalonDock:DockingManager.LayoutItemTemplateSelector> 

    <avalonDock:DockingManager.Theme> 
    <avalonDock:VS2010Theme/> 
    </avalonDock:DockingManager.Theme> 
    <avalonDock:LayoutRoot> 
    <avalonDock:LayoutPanel Orientation="Horizontal"> 
     <avalonDock:LayoutAnchorablePane DockWidth="400"> 
     <avalonDock:LayoutAnchorable Title="Scope(s) selection" x:Name="PanelScopeSelection" IsVisible="True"> 
      <scopeSelection:UserControlSelectStudyScope x:Name="ToolScopeSelection"/> 
     </avalonDock:LayoutAnchorable> 
     </avalonDock:LayoutAnchorablePane> 
     <avalonDock:LayoutDocumentPane/> 
     <avalonDock:LayoutAnchorablePane DockWidth="150"> 
     <avalonDock:LayoutAnchorable Title="Properties" x:Name="PanelScopePropertyGrid"> 
      <!--<multiSimAnalysis:UserControlPropertyGrid x:Name="ToolPropertyGrid" />--> 
      <xctk:PropertyGrid x:Name="ToolPropertyGrid" SelectedObject="{Binding ActiveObject}" /> 
     </avalonDock:LayoutAnchorable> 
     </avalonDock:LayoutAnchorablePane> 
    </avalonDock:LayoutPanel> 
    </avalonDock:LayoutRoot> 
</avalonDock:DockingManager> 

回答

2

其實我覺得不能接受的解決辦法。 它真的扭曲了。

我只把它作爲參考。應該有一個乾淨的方式來做到這一點。

// ************************************************************************ 
    private void DockingManager_DocumentClosing(object sender, Xceed.Wpf.AvalonDock.DocumentClosingEventArgs e) 
    { 
     e.Document.CanClose = false; 

     DocumentModel documentModel = e.Document.Content as DocumentModel; 
     if (documentModel != null) 
     { 
      Dispatcher.BeginInvoke(new Action(() => this.Model.DocumentItems.Remove(documentModel)), DispatcherPriority.Background); 
     } 
    } 
+0

如果您不使用MVVM,有沒有辦法解決這個問題? –

+0

我不知道? (抱歉,延遲...我剛剛看到你的問題) –

+0

這是一個遠射,但這個事件是從來沒有給我打過電話,其他人有沒有類似的經歷? –

-1

註冊IsVisibleChanged。

void layoutFPR_Hidden(object sender, EventArgs e) 
{ 
    LayoutAnchorable window = (LayoutAnchorable)sender; 
    YourClass content = window.Content as YourClass; 

    // Close the object 
    content = null; 
    ((LayoutAnchorable)sender).Close(); 

} 
0

我發現,在LayoutDocumentLayoutAnchorablePane,同時應用該設置:CanClose="False"CanFloat="False"

它刪除關閉按鈕。

<avalonDock:LayoutDocument Title="Board" 
          ContentId="Board" 
          CanClose="False" 
          CanFloat="False"> 
</avalonDock:LayoutDocument>