Click here to Skip to main content
15,867,278 members
Articles / Desktop Programming / WPF

Integrated Help System in a WPF Application

Rate me:
Please Sign up or sign in to vote.
4.78/5 (15 votes)
20 Jun 2013CPOL6 min read 43.1K   2K   45  
Quick guideline to understand a workflow of screen without reading long and boring(!!) documentation guide
This is a quick guideline to get rid of reading long and boring documentation and give you very basic information about a screen.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using IntegratedHelpInWPF.Commands;

namespace IntegratedHelpInWPF
{
    public class MainViewModel : BaseViewModel
    {        
        public MainViewModel()
        {
            this.View = new MainWindow {ViewModel = this};
            _wishlistCommand = new DelegateCommand(ExecuteWishlistCommand);
        }

        private void ExecuteWishlistCommand(object obj)
        {
            var wishViewModel = new WishlistViewModel();
            wishViewModel.ShowDialog();
        }

        private DynamicHelpViewModel _dynamicHelpViewModel;

        public DynamicHelpViewModel DynamicHelpViewModel
        {
            get
            {
                if (_dynamicHelpViewModel == null)
                {
                    _dynamicHelpViewModel = new DynamicHelpViewModel();
                }
                return _dynamicHelpViewModel;
            }
            set { _dynamicHelpViewModel = value; NotifyPropertyChanged(()=> DynamicHelpViewModel); }
        }

        private readonly ICommand _wishlistCommand;
        public ICommand WishlistCommand
        {
            get { return _wishlistCommand; }
        }
    }

    public abstract class BaseViewModel : INotifyPropertyChanged
    {
        private IBaseView _view;
        public IBaseView View
        {
            get { return _view; }
            set { _view = value; NotifyPropertyChanged(()=>View); }
        }
        public virtual bool CleanUpResource()
        {
            return true;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Notify using pre-made PropertyChangedEventArgs
        /// </summary>
        /// <param name="args"></param>
        protected void NotifyPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, args);
            }
        }

        /// <summary>
        /// Notify using String property name
        /// </summary>
        protected void NotifyPropertyChanged(String propertyName)
        {
            this.VerifyPropertyName(propertyName);
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public static string GetPropertyName(Expression<Func<Object>> propertyExpression)
        {
            var lambda = propertyExpression as LambdaExpression;
            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = lambda.Body as UnaryExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
            else
            {
                memberExpression = lambda.Body as MemberExpression;
            }
            var constantExpression = memberExpression.Expression as ConstantExpression;
            var propertyInfo = memberExpression.Member as PropertyInfo;

            return propertyInfo.Name;
        }

        protected void NotifyPropertyChanged(Expression<Func<Object>> propertyExpression)
        {
            string propertyName = GetPropertyName(propertyExpression);

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Debugging Aides

        /// <summary>
        /// Warns the developer if this object does not have
        /// a public property with the specified name. This 
        /// method does not exist in a Release build.
        /// </summary>
        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        public void VerifyPropertyName(string propertyName)
        {
#if !SILVERLIGHT
            // Verify that the property name matches a real,  
            // public, instance property on this object.
            if (TypeDescriptor.GetProperties(this)[propertyName] == null)
            {
                string msg = "Invalid property name: " + propertyName;

                if (this.ThrowOnInvalidPropertyName)
                    throw new Exception(msg);
                else
                    Debug.Fail(msg);
            }
#endif
        }

        /// <summary>
        /// Returns whether an exception is thrown, or if a Debug.Fail() is used
        /// when an invalid property name is passed to the VerifyPropertyName method.
        /// The default value is false, but subclasses used by unit tests might 
        /// override this property's getter to return true.
        /// </summary>
        protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

        #endregion // Debugging Aides
    }

    public interface IMainView : IBaseView
    {
    }

    public interface IBaseView
    {
        BaseViewModel ViewModel { set;  }
    }
}

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
Team Leader PracticePRO Software Systems Inc
United States United States
In my childhood, my uncle has shown me how to see the cloud in a close look and I understand that one can draw some elements of the Earth in the sky-canvas if he/she wants to. After that the cloud becomes closer to me and It teaches me one thing that, a deeper-look to something will give you some clues to draw your imagination. You can able to see that one which you have build-up in your mind.

Years past, I have started my career as a software engineer and has been looking for passion in my coding and development which I should be to enjoy my profession and has started asking myself- 'am I doing any engineering here?!' Is my code becoming that thing which I have designed in my mind? So to find that answer I have tried that old solution here... I have decided to come closer to my code and start analyzing them. And it is really working for me and at least it gives me the confidence that I can build something that I really want to. I can draw my thinking there through my code and can build-up my vision that I have designed in my mind. It also helps me to think out of the box, solve each problems by making blocks and make me careful on each steps.

• Morshed's Technical Blog site: http://morshedanwar.wordpress.com/

• Morshed's Technical articles those are published in Codeproject site: http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2992452

• Morshed's Linkedin profile: http://www.linkedin.com/in/morshedanwar

• Morshed's Facebook Profile : http://www.facebook.com/morshed.pulok

Beside all these I like to do - photography and music. Here is my Flickr photos : http://www.flickr.com/photos/morshed_anwar/

Comments and Discussions