Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Integrated Help system in a WPF Application

, 20 Jun 2013
Quick guideline to understand a workflow of screen without reading long and boring(!!) documentation guide.
integratedhelpinwpf-noexe.zip
IntegratedHelpInWPF
IntegratedHelpInWPF
AttachProperties
bin
Debug
DynamicHelpReference
IntegratedHelpInWPF.vshost.exe.manifest
Commands
DynamicHelpReference
IntegratedHelpInWPF.csproj.user
Languages
Popup
Properties
Settings.settings
Resources
integratedhelpinwpf.zip
IntegratedHelpInWPF.exe
IntegratedHelpInWPF.vshost.exe
IntegratedHelpInWPF.vshost.exe.manifest
Release
IntegratedHelpInWPF.csproj.user
obj
Debug
Resources
TempPE
Settings.settings
IntegratedHelpInWPF_Revised_-noexe.zip
IntegratedHelpInWPF.vshost.exe.manifest
Converter
IntegratedHelpInWPF.csproj.user
DetailsHelpView.baml
DynamicHelpView.baml
IntegratedHelpInWPF.g.resources
IntegratedHelpInWPF.Properties.Resources.resources
IntegratedHelpInWPF_MarkupCompile.lref
MainWindow.baml
CommonResource.baml
WishlistView.baml
Settings.settings
IntegratedHelpInWPF.v11.suo
IntegratedHelpInWPF_Revised_.zip
Assembly
System.Windows.Interactivity.dll
IntegratedHelpInWPF.exe
IntegratedHelpInWPF.pdb
IntegratedHelpInWPF.vshost.exe
IntegratedHelpInWPF.vshost.exe.manifest
System.Windows.Interactivity.dll
IntegratedHelpInWPF.csproj.user
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
DetailsHelpView.baml
DynamicHelpView.baml
IntegratedHelpInWPF.csproj.GenerateResource.Cache
IntegratedHelpInWPF.csprojResolveAssemblyReference.cache
IntegratedHelpInWPF.exe
IntegratedHelpInWPF.g.resources
IntegratedHelpInWPF.pdb
IntegratedHelpInWPF.Properties.Resources.resources
IntegratedHelpInWPF_MarkupCompile.cache
IntegratedHelpInWPF_MarkupCompile.i.cache
IntegratedHelpInWPF_MarkupCompile.lref
MainWindow.baml
CommonResource.baml
Properties.Resources.Designer.cs.dll
WishlistView.baml
Settings.settings
IntegratedHelpInWPF.v11.suo
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Threading;

namespace JerichoCOREService.View.Popup
{
    public class Adorners
    {
        // Template attached property

        public static readonly DependencyProperty TemplateProperty =
            DependencyProperty.RegisterAttached("Template", typeof(ControlTemplate), typeof(Adorners),
            new PropertyMetadata(TemplateChanged));

        public static ControlTemplate GetTemplate(UIElement target)
        {
            return (ControlTemplate)target.GetValue(TemplateProperty);
        }
        public static void SetTemplate(UIElement target, ControlTemplate value)
        {
            target.SetValue(TemplateProperty, value);
        }
        private static void TemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UpdateAdroner((UIElement)d, GetIsVisible((UIElement)d), (ControlTemplate)e.NewValue, GetData((UIElement)d));
        }

        // IsVisible attached property

        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(Adorners),
            new PropertyMetadata(IsVisibleChanged));
        public static bool GetIsVisible(UIElement target)
        {
            return (bool)target.GetValue(IsVisibleProperty);
        }
        public static void SetIsVisible(UIElement target, bool value)
        {
            target.SetValue(IsVisibleProperty, value);
        }
        private static void IsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UpdateAdroner((UIElement)d, (bool)e.NewValue, GetTemplate((UIElement)d), GetData((UIElement)d));
        }


        // IsVisible attached property

        public static readonly DependencyProperty DataProperty =
            DependencyProperty.RegisterAttached("Data", typeof(object), typeof(Adorners),
            new PropertyMetadata(IsDataChanged));
        public static object GetData(UIElement target)
        {
            return target.GetValue(DataProperty);
        }
        public static void SetData(UIElement target, object value)
        {
            target.SetValue(DataProperty, value);
        }
        private static void IsDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UpdateAdroner((UIElement)d, GetIsVisible((UIElement)d), GetTemplate((UIElement)d), e.NewValue );
        }

        // InternalAdorner attached property

        public static readonly DependencyProperty InternalAdornerProperty =
            DependencyProperty.RegisterAttached("InternalAdorner", typeof(ControlAdorner), typeof(Adorners));

        public static ControlAdorner GetInteranlAdorner(DependencyObject target)
        {
            return (ControlAdorner)target.GetValue(InternalAdornerProperty);
        }
        public static void SetInternalAdorner(DependencyObject target, ControlAdorner value)
        {
            target.SetValue(InternalAdornerProperty, value);
        }

        // Actually do all the work:

        private static void UpdateAdroner(UIElement adorned)
        {
            UpdateAdroner(adorned, GetIsVisible(adorned), GetTemplate(adorned), GetData(adorned));
        }

        private static void UpdateAdroner(UIElement adorned, bool isVisible, ControlTemplate controlTemplate, object data)
        {
            var layer = AdornerLayer.GetAdornerLayer(adorned);

            if (layer == null)
            {
                // if we don't have an adorner layer it's probably
                // because it's too early in the window's construction
                // Let's re-run at a slightly later time
                Dispatcher.CurrentDispatcher.BeginInvoke(
                    DispatcherPriority.Loaded,
                    (Action) ( ()=> UpdateAdroner(adorned)));
                return;
            }

            var existingAdorner = GetInteranlAdorner(adorned);

            if (existingAdorner == null)
            {
                if (controlTemplate != null && isVisible)
                {
                    // show
                    var newAdorner = new ControlAdorner(adorned)
                                         {
                                             Child = new Control()
                                             {
                                                 Template = controlTemplate,
                                                 Focusable = false,
                                                 DataContext = data
                                             }
                                         };
                    layer.Add(newAdorner);
                    SetInternalAdorner(adorned, newAdorner);
                }
            }
            else
            {
                if (controlTemplate != null && isVisible)
                {
                    // switch template
                    Control ctrl = existingAdorner.Child;
                    ctrl.Template = controlTemplate;
                    ctrl.DataContext = data;
                }
                else
                {
                    // hide
                    existingAdorner.Child = null;
                    layer.Remove(existingAdorner);
                    SetInternalAdorner(adorned, null);
                }
            }
        }
    }
}

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)

About the Author

Morshed Anwar
Team Leader Adaptive Enterprise Limited (www.ael-bd.com)
Bangladesh Bangladesh
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 20 Jun 2013
Article Copyright 2012 by Morshed Anwar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid