Click here to Skip to main content
13,086,662 members (56,408 online)
Click here to Skip to main content

Stats

132.1K views
7.7K downloads
131 bookmarked
Posted 10 Aug 2011

AvalonDock and MVVM

, 9 Oct 2011
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)

Share

About the Author

Ashley Davis
Team Leader Code Capers
Australia Australia
I have a background in video game development with many years in serious games, simulations and VR. Making technology work for business is what I do: building bespoke software solutions that span multiple platforms.

I have many 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-moving and high-pressure environments. I know how to focus and prioritize so that the important things get done.

I am a passionate technologist and agile practitioner. Agile techniques have bought me much professional and personal benefit. I have had great success with TDD and am convinced of its ability to produce better code that stabilizes more quickly while being evolved rapidly. I'm a fan of functional programming and its potential for safety, predictability & concurrency.

I contribute to open source and have founded multiple industry groups in Brisbane.

Linkedin:

https://www.linkedin.com/in/ashleydavis75/

Market Wizard:

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

Data Forge:

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

Web

www.codecapers.com.au

Meetups

www.meetup.com/Game-Technology-Brisbane
www.meetup.com/Game-development-Brisbane
www.meetup.com/brisbane-unity-developers

Open source

https://github.com/codecapers
https://github.com/Real-Serious-Games
https://github.com/data-forge

Blogs

www.what-could-possibly-go-wrong.com
www.the-data-wrangler.com

Skills

Management of development teams & projects
Coaching developers
Making sense of technology for senior management
Developing technical strategies
Data wrangling & visualization.
Desktop, cloud/web & mobile apps.
Game dev, serious games, simulations & VR experiences.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Ashley Davis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid