【问题标题】:Avalondock MVVM LayoutAvalondock MVVM 布局
【发布时间】:2016-02-08 13:07:41
【问题描述】:

所以问题在but not answerednot answered the way I would like 之前被问过

我知道如何创建我想要实现的布局,在 XAML 中使用 LayoutAnchorablePaneGroupLayoutAnchorablePaneLayoutDocument,但我想以 MVVM 方式使用 Avalondock,将我的 XAML 减少到:

<avalonDock:DockingManager x:Name="dockingManager" 
                                       DataContext="{Binding DockManagerViewModel}"
                                       DocumentsSource="{Binding Documents}"
                                       AnchorablesSource="{Binding Anchorables}"
                                       Loaded="dockingManager_Loaded" 
                                       Unloaded="dockingManager_Unloaded">

填写DocumentsAnchorables 会使所需的窗口出现在停靠管理器中,但我不知道如何确定它们将出现的位置。

如何指定一些规则(最好在 XAML 中)来构建特定的布局,而不会失去 MVVM 分离?

例如:类型 A 的对象都应该放在右侧的 LayoutAnchorablePane 中,类型 B 的对象都放在左侧的 LayoutAnchorablePane 中等等。

提前致谢。

【问题讨论】:

  • 这是一个相当宽泛的问题,除了你引用的那些答案,如stackoverflow.com/a/32567244/2224701,或codeproject.com/Articles/239342/AvalonDock-and-MVVM
  • 是的,我看过那些。链接 1 对于学习一般的 Avalondock MVVM 方法非常有用,但它没有回答我的问题(或者我不明白答案),而我不确定链接 2 是否仍然相关。由于它不使用DataContextDocumentSourceAnchorablesSoruce,因此使旧的 Avalondock 版本与 MVVM 兼容可能是一个拐杖。我希望使用 AvalonDock 2.0 可能会有更简单的方法。
  • 那么您已经遵循了 CodePlex AvalonDock MVVM 示例应用程序方法并且您遇到了 Anchorables 的一些特定问题? but I don't see how I can secify the place in which they will appear - place 到底是什么意思?你需要实现一些特定的初始布局吗?考虑让你的问题更具体一点。

标签: c# wpf mvvm avalondock xceed


【解决方案1】:

我也遇到了同样的情况。并找到了一个棘手但对我有用的解决方案。

遵循Solution on Code Project 并实现了保存和加载布局。

请注意,当应用程序第一次启动时,它没有布局,因此您需要创建一个具有所需布局的 XML,然后您可以加载保存的布局。希望这可以帮助。

对接管理器示例:

  <xcad:DockingManager x:Name="DockingManagerDockView"
                         AnchorablesSource="{Binding AnchorableSource}" 
                         DocumentsSource="{Binding DocumentSource}" 
                         Utility:AvalonDockLayoutSerializer.SaveLayoutCommand="{Binding SaveLayoutCommandOnExit}"
                         Utility:AvalonDockLayoutSerializer.LoadLayoutCommand="{Binding LoadLayoutCommand}">       
    <xcad:DockingManager.Theme>
        <xcad:MetroTheme />
    </xcad:DockingManager.Theme>
    <xcad:DockingManager.LayoutUpdateStrategy>
        <Pane:LayoutInitializer/>
    </xcad:DockingManager.LayoutUpdateStrategy>
    <xcad:DockingManager.Resources>            
        <DataTemplate DataType="{x:Type ViewModels:ExplorerViewModel}">
            <Views:ExplorerView />
        </DataTemplate>            
        <DataTemplate DataType="{x:Type ViewModels:TableOfContentViewModel}">
            <Views:TableOfContentView x:Name="TOCView" Focusable="True">
                <Views:TableOfContentView.InputBindings>
                    <KeyBinding Key="F5" Command="{Binding GridF5Command}"/>
                </Views:TableOfContentView.InputBindings>
            </Views:TableOfContentView>
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:PropertyViewModel}">
            <Views:PropertyView />
        </DataTemplate>           
        <DataTemplate DataType="{x:Type ViewModels:SearchViewModel}">
            <Views:SearchPanel />
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:DocumentViewModel}">
            <Views:DocumentView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type ViewModels:ReIndexPanelViewModel}">
            <Views:ReIndexPanel />
        </DataTemplate>
    </xcad:DockingManager.Resources>       
    <xcad:DockingManager.LayoutItemContainerStyleSelector>
        <Pane:PanesStyleSelector>
            <Pane:PanesStyleSelector.ToolStyle>
                <Style TargetType="{x:Type xcad:LayoutAnchorableItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}"/>
                    <Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter={x:Static Visibility.Hidden}}"/>
                    <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                    <Setter Property="FlowDirection" Value="LeftToRight"/>
                    <Setter Property="UseLayoutRounding" Value="False"/>
                    <Setter Property="IconSource" Value="{Binding Model.IconSource}"/>
                </Style>
            </Pane:PanesStyleSelector.ToolStyle>
            <Pane:PanesStyleSelector.FileStyle>
                <Style TargetType="{x:Type xcad:LayoutItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}"/>
                    <Setter Property="ToolTip" Value="{Binding Model.FilePath}"/>
                    <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                    <Setter Property="CanClose" Value="False"/>                        
                </Style>
            </Pane:PanesStyleSelector.FileStyle>
        </Pane:PanesStyleSelector>
    </xcad:DockingManager.LayoutItemContainerStyleSelector>
    <xcad:LayoutRoot>
        <xcad:LayoutPanel Orientation="Horizontal">                
                <xcad:LayoutAnchorablePaneGroup>
                    <xcad:LayoutAnchorablePane Name="Explorer" DockMinWidth="250"/> 
                    <xcad:LayoutAnchorablePane Name="TOC" DockMinWidth="500"/>
                    <xcad:LayoutAnchorablePane Name="Property" DockMinWidth="300" />
                    <xcad:LayoutAnchorablePane Name="Search" DockMinWidth="300" />
                    <xcad:LayoutAnchorablePane Name="ReIndex" DockMinHeight="300" />
                </xcad:LayoutAnchorablePaneGroup>
            <xcad:LayoutDocumentPaneGroup >
                <xcad:LayoutDocumentPane/>
            </xcad:LayoutDocumentPaneGroup>
        </xcad:LayoutPanel>            
    </xcad:LayoutRoot>
</xcad:DockingManager>

【讨论】:

  • 是的,我已经看到了,但是我对这个解决方案有一个问题:如果您添加一个新文档,您如何指定应该在哪个 DocumentPane 中创建它?
  • 抱歉,想新建一行,Enter 添加了评论 :)
  • 好吧,我已经在答案中添加了我的对接管理器是如何完成的。
  • 好的,因为解决方案中有一个名为 LayoutInitialize 的单独类。该类说明了您的新文档的去向。
  • 当然。 LayoutInitilizer 是您需要查看的。
猜你喜欢
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多