Click here to Skip to main content
13,734,094 members
Click here to Skip to main content

Tagged as

Stats

151.9K views
8.1K downloads
134 bookmarked
Posted 10 Aug 2011
Licenced CPOL

AvalonDock and MVVM

, 9 Oct 2011
Demonstrates a technique for integrating AvalonDock with an MVVM application.
using System;
using System.Text;

namespace SampleApp.ViewModels
{
    /// <summary>
    /// The view-model for a text file document.
    /// </summary>
    public class TextFileDocumentViewModel : AbstractViewModel
    {
        /// <summary>
        /// The default name for untitled files.
        /// </summary>
        public static readonly string UntitledFileName = "Untitled.txt";

        /// <summary>
        /// The file path of the document.
        /// </summary>
        private string filePath = string.Empty;

        /// <summary>
        /// The text of the document.
        /// </summary>
        private string text = string.Empty;

        /// <summary>
        /// Set to 'true' when the document is modified and needs to be saved.
        /// </summary>
        private bool isModified = false;

        public TextFileDocumentViewModel(string filePath, string text, bool isModified)
        {
            this.filePath = filePath;
            this.text = text;
            this.isModified = isModified;
        }

        /// <summary>
        /// The title of the document.
        /// </summary>
        public string Title
        {
            get
            {
                var title = new StringBuilder();
                title.Append(this.FileName);

                if (this.IsModified)
                {
                    title.Append("*");
                }

                return title.ToString();
            }
        }

        /// <summary>
        /// Tooltip to display in the UI.
        /// </summary>
        public string ToolTip
        {
            get
            {
                var toolTip = new StringBuilder();
                if (string.IsNullOrEmpty(this.FilePath))
                {
                    toolTip.Append(UntitledFileName);
                }
                else
                {
                    toolTip.Append(this.FilePath);
                }

                if (this.IsModified)
                {
                    toolTip.Append("*");
                }

                return toolTip.ToString();
            }
        }

        /// <summary>
        /// Name of the file (with the path stripped off).
        /// </summary>
        public string FileName
        {
            get
            {
                if (string.IsNullOrEmpty(this.FilePath))
                {
                    return UntitledFileName;
                }

                return System.IO.Path.GetFileName(this.FilePath);
            }
        }

        /// <summary>
        /// The file path of the document.
        /// </summary>
        public string FilePath
        {
            get
            {
                return filePath;
            }
            set
            {
                if (filePath == value)
                {
                    return;
                }

                filePath = value;

                OnPropertyChanged("FilePath");
                OnPropertyChanged("FileName");
                OnPropertyChanged("Title");
                OnPropertyChanged("ToolTip");
            }
        }

        /// <summary>
        /// The path to the directory that contains the file.
        /// </summary>
        public string DirectoryPath
        {
            get
            {
                if (string.IsNullOrEmpty(this.FilePath))
                {
                    return string.Empty;
                }

                return System.IO.Path.GetDirectoryName(this.FilePath);  
            }
        }

        /// <summary>
        /// The text of the document.
        /// </summary>
        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                if (text == value)
                {
                    return;
                }

                text = value;

                this.IsModified = true;

                OnPropertyChanged("Text");
            }
        }

        /// <summary>
        /// Set to 'true' when the document is modified and needs to be saved.
        /// </summary>
        public bool IsModified 
        {
            get
            {
                return isModified;
            }
            set
            {
                if (isModified == value)
                {
                    return;
                }

                isModified = value;

                OnPropertyChanged("IsModified");
                OnPropertyChanged("Title");
                OnPropertyChanged("ToolTip");

                if (this.IsModifiedChanged != null)
                {
                    this.IsModifiedChanged(this, EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Event raised when the IsModified has changed.
        /// </summary>
        public event EventHandler<EventArgs> IsModifiedChanged;
    }
}

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
Chief Technology Officer OldView
Australia Australia
My book Data Wrangling with JavaScript is live:
https://www.manning.com/books/data-wrangling-with-javascript

Also please check out Data-Forge Notebook, my notebook-style app for data analysis and visualizatiion in JavaScript:
http://www.data-forge-notebook.com/

I have a long history in software development with many years in apps, web apps, serious games, sims 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 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.

I am a passionate technologist and agile practitioner. 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.

I'm also a keen systems trader, currently trading stocks on the ASX.

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/Real-Serious-Games

Blogs

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

Skills

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

See Linked-in for full history:

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

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Ashley Davis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid