Click here to Skip to main content
13,150,607 members (30,897 online)
Click here to Skip to main content

Stats

133.9K 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace AvalonDockMVVM
{
    /// <summary>
    /// Helper class for lookup of data-templates.
    /// </summary>
    internal static class DataTemplateUtils
    {
        /// <summary>
        /// Find a DataTemplate for the specified type in the visual-tree.
        /// </summary>
        public static DataTemplate FindDataTemplate(Type type, FrameworkElement element)
        {
            var dataTemplate = element.TryFindResource(new DataTemplateKey(type)) as DataTemplate;
            if (dataTemplate != null)
            {
                return dataTemplate;
            }

            if (type.BaseType != null && type.BaseType != typeof(object))
            {
                dataTemplate = FindDataTemplate(type.BaseType, element);
                if (dataTemplate != null)
                {
                    return dataTemplate;
                }
            }

            foreach (var interfaceType in type.GetInterfaces())
            {
                dataTemplate = FindDataTemplate(interfaceType, element);
                if (dataTemplate != null)
                {
                    return dataTemplate;
                }
            }

            return null;
        }

        /// <summary>
        /// Find a data-template for the specified type and instance a visual from it.
        /// </summary>
        public static FrameworkElement InstanceTemplate(Type type, FrameworkElement element, object dataContext)
        {
            var dataTemplate = FindDataTemplate(type, element);
            if (dataTemplate == null)
            {
                return null;
            }

            return InstanceTemplate(dataTemplate, dataContext);
        }

        /// <summary>
        /// Instance a visual element from a data template.
        /// </summary>
        public static FrameworkElement InstanceTemplate(DataTemplate dataTemplate, object dataContext)
        {
            var element = (FrameworkElement)dataTemplate.LoadContent();
            element.DataContext = dataContext;
            return element;
        }
    }
}

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
Web03 | 2.8.170924.2 | Last Updated 10 Oct 2011
Article Copyright 2011 by Ashley Davis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid