问题
I am trying to use AvalonDock in my wpf application which is an MVVM application. Looking around I could not find any sample application showing how can I do this.
AlavonDock says that it has native support for MVVM, so it should be easy to support mvvm, but there is no sample code.
My questions are:
- How to write xaml that has a document manager and it is binded to viewmodel?
- How to add a new document to panel in this scenario?
- How can I get information about layout from documentmanegr (if it is possible).
回答1:
there is an Example App in the CodePlex Source of AvalonDock - it's not included in the normal download. You'll need to go to the Source Control page and click on 'Download'.
Additionally, I've written an example App, that you can also use to get started, I wrote a quick blog post describing it and put it on GitHub.
Basically, you can set the LayoutItemContainerStyle
to bridge the gap between the View and your ViewModel, for example:
<Window ...
xmlns:dock="http://schemas.xceed.com/wpf/xaml/avalondock"
xmlns:dockctrl="clr-namespace:Xceed.Wpf.AvalonDock.Controls;assembly=Xceed.Wpf.AvalonDock"
>
...
<dock:DockingManager DataContext="{Binding DockManagerViewModel}"
DocumentsSource="{Binding Documents}" >
<dock:DockingManager.LayoutItemContainerStyle>
<!-- you can add additional bindings from the layoutitem to the DockWindowViewModel -->
<Style TargetType="{x:Type dockctrl:LayoutItem}">
<Setter Property="Title" Value="{Binding Model.Title}" />
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
<Setter Property="CanClose" Value="{Binding Model.CanClose}" />
</Style>
</dock:DockingManager.LayoutItemContainerStyle>
</dock:DockingManager>
</Window>
In this example, DockManagerViewModel has a property 'Documents' with a collection of ViewModels that have a Title, CloseCommand and CanClose property.
来源:https://stackoverflow.com/questions/23406451/sample-code-to-show-how-to-use-avalondock-in-an-mvvm-application