Click here to Skip to main content
15,888,610 members
Articles / Desktop Programming / WPF

AvalonDock and MVVM

Rate me:
Please Sign up or sign in to vote.
4.87/5 (50 votes)
9 Oct 2011CPOL34 min read 214.4K   9.2K   135  
Demonstrates a technique for integrating AvalonDock with an MVVM application.
<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SampleApp"
        xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"
        xmlns:ViewModels="clr-namespace:SampleApp.ViewModels;assembly=SampleApp.ViewModels"
        xmlns:AvalonDockMVVM="clr-namespace:AvalonDockMVVM;assembly=AvalonDockMVVM"
        x:Name="mainWindow"
        Title="{Binding Title}" 
        Closing="Window_Closing"
        >
    <Window.Resources>

        <RoutedUICommand x:Key="Commands.NewFile" />
        <RoutedUICommand x:Key="Commands.OpenFile" />
        <RoutedUICommand x:Key="Commands.SaveFile" />
        <RoutedUICommand x:Key="Commands.SaveFileAs" />
        <RoutedUICommand x:Key="Commands.SaveAllFiles" />
        <RoutedUICommand x:Key="Commands.CloseFile" />
        <RoutedUICommand x:Key="Commands.CloseAllFiles" />
        <RoutedUICommand x:Key="Commands.ShowAllPanes" />
        <RoutedUICommand x:Key="Commands.HideAllPanes" />
        <RoutedUICommand x:Key="Commands.Exit" />
        
        <!-- 
        Data template for displaying tabbed documents.
        This is really simple they are just represented by an AvalonDock
        DocumentContent that contains a simple WPF TextBox.
        -->           
        <DataTemplate
            DataType="{x:Type ViewModels:TextFileDocumentViewModel}"
            >
            <ad:DocumentContent
                Title="{Binding Title}"      
                ToolTip="{Binding ToolTip}"
                >                
                <TextBox
                    Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" TextWrapping="Wrap" />
            </ad:DocumentContent>
        </DataTemplate>

        <!--
        The DataTemplate for the 'Open Documents' pane.
        This uses an AvalonDock DockableContent that contains an instance of the
        OpenDocumentPaneView user control.
        -->
        <DataTemplate
            DataType="{x:Type ViewModels:OpenDocumentsPaneViewModel}"
            >
            <ad:DockableContent
                x:Name="openDocumentsPane"
                Title="Open Documents"
                AvalonDockMVVM:AvalonDockHost.IsPaneVisible="{Binding IsVisible}"
                >
                <local:OpenDocumentsPaneView />
            </ad:DockableContent>
        </DataTemplate>

        <!-- 
        The DataTemplate for the 'Document Overview' pane.
        This uses an AvalonDock DockableContent that contains an instance of the 
        DocumentOverviewView user control.
        -->
        <DataTemplate
            DataType="{x:Type ViewModels:DocumentOverviewPaneViewModel}"
            >
            <ad:DockableContent
                x:Name="documentOverviewPane"
                Title="Document Overview"
                AvalonDockMVVM:AvalonDockHost.IsPaneVisible="{Binding IsVisible}"
                >
                <local:DocumentOverviewPaneView />
            </ad:DockableContent>
        </DataTemplate>

    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding 
            Command="{StaticResource Commands.NewFile}"
            Executed="NewFile_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.OpenFile}"
            Executed="OpenFile_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.SaveFile}"
            Executed="SaveFile_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.SaveFileAs}"
            Executed="SaveFileAs_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.SaveAllFiles}"
            Executed="SaveAllFiles_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.CloseFile}"
            Executed="CloseFile_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.CloseAllFiles}"
            Executed="CloseAllFiles_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.ShowAllPanes}"
            Executed="ShowAllPanes_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.HideAllPanes}"
            Executed="HideAllPanes_Executed"
            />
        <CommandBinding 
            Command="{StaticResource Commands.Exit}"
            Executed="Exit_Executed"
            />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding
			Key="N"
            Modifiers="Control"
			Command="{StaticResource Commands.NewFile}"
			/>
        <KeyBinding
			Key="O"
            Modifiers="Control"
			Command="{StaticResource Commands.OpenFile}"
			/>
        <KeyBinding
			Key="S"
            Modifiers="Control"
			Command="{StaticResource Commands.SaveFile}"
			/>
    </Window.InputBindings>
    <DockPanel>
        <Menu
            DockPanel.Dock="Top"
            >
            <MenuItem
                Header="_File"
                >
                <MenuItem
                    Header="_New File"
                    Command="{StaticResource Commands.NewFile}"
                    />
                <MenuItem
                    Header="_Open File"
                    Command="{StaticResource Commands.OpenFile}"
                    />
                <Separator />
                <MenuItem
                    Header="_Close File"
                    Command="{StaticResource Commands.CloseFile}"
                    />
                <MenuItem
                    Header="C_lose All Files"
                    Command="{StaticResource Commands.CloseAllFiles}"
                    />
                <Separator />
                <MenuItem
                    Header="_Save File"
                    Command="{StaticResource Commands.SaveFile}"
                    />
                <MenuItem
                    Header="Save File _As"
                    Command="{StaticResource Commands.SaveFileAs}"
                    />
                <MenuItem
                    Header="Save All F_iles"
                    Command="{StaticResource Commands.SaveAllFiles}"
                    />
                <Separator />
                <MenuItem
                    Header="E_xit"
                    Command="{StaticResource Commands.Exit}"
                    />
            </MenuItem>
            <MenuItem
                Header="_View"
                >
                <MenuItem
                    Header="_Document Overview"
                    IsChecked="{Binding DocumentOverviewPaneViewModel.IsVisible}"
                    IsCheckable="True"
                    />
                <MenuItem
                    Header="_Open Documents"
                    IsChecked="{Binding OpenDocumentsPaneViewModel.IsVisible}"
                    IsCheckable="True"
                    />
                <Separator />
                <MenuItem
                    Header="_Show All"
                    Command="{StaticResource Commands.ShowAllPanes}"
                    />
                <MenuItem
                    Header="_Hide All"
                    Command="{StaticResource Commands.HideAllPanes}"
                    />
            </MenuItem>
        </Menu>

        <AvalonDockMVVM:AvalonDockHost
            x:Name="avalonDockHost"
            Panes="{Binding Panes}"
            Documents="{Binding Documents}"
            ActiveDocument="{Binding ActiveDocument}"
            ActivePane="{Binding ActivePane}"
            AvalonDockLoaded="avalonDockHost_AvalonDockLoaded"
            DocumentClosing="avalonDockHost_DocumentClosing"
            />
    </DockPanel>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Australia Australia
Software craftsman | Author | Writing rapidfullstackdevelopment.com - Posting about how to survive and flourish as a software developer

Follow on Twitter for news and updates: https://twitter.com/codecapers

I'm writing a new book: Rapid Fullstack Development. Learn from my years of experience and become a better developer.

My second book, Bootstrapping Microservices, is a practical and project-based guide to building distributed applications with microservices.

My first book Data Wrangling with JavaScript is a comprehensive overview of working with data in JavaScript.

Data-Forge Notebook is my notebook-style application for data transformation, analysis and transformation in JavaScript.

I have a long history in software development with many years in apps, web apps, backends, serious games, simulations and VR. Making technology work for business is what I do: building bespoke software solutions that span multiple platforms.

I have years of experience managing development teams, preparing technical strategies and creation of software products. I can explain complicated technology to senior management. I have delivered cutting-edge products in fast-paced and high-pressure environments. I know how to focus and prioritize to get the important things done.

Author

- Rapid Fullstack Development
- Bootstrapping Microservices
- Data Wrangling with JavaScript

Creator of Market Wizard

- https://www.market-wizard.com.au/

Creator of Data-Forge and Data-Forge Notebook

- http://www.data-forge-js.com
- http://www.data-forge-notebook.com

Web

- www.codecapers.com.au

Open source

- https://github.com/ashleydavis
- https://github.com/data-forge
- https://github.com/data-forge-notebook


Skills

- Quickly building MVPs for startups
- Understanding how to get the most out of technology for business
- Developing technical strategies
- Management and coaching of teams & projects
- Microservices, devops, mobile and fullstack software development

Comments and Discussions