Welcome to MSDN Blogs Sign in | Join | Help

Data See, Data Do

Mike Hillberg's Blog on Wpf (.Net and Silverlight)

Syndication

News

All postings are provided "As Is" with no warranties, and confers no rights. Opinions and views expressed here are not necessarily those of Microsoft Corporation.


A Context-Sensitive Help Provider in Wpf

Here's an example of a way to add context-sensitive help to your application. 

The main idea is to simply use the built-in ApplicationCommands.Help command.  This command is already tied to the F1 key, and so executes when you hit F1, and tells your command handler what element the user was on when it was hit.

First, start with this little application:

<Window x:Class="ContextSensitiveHelp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300" >

 

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width='auto'/>

      <ColumnDefinition Width='*'/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height='auto'/>

      <RowDefinition Height='auto' />

    </Grid.RowDefinitions>

 

    <TextBlock Grid.Column='0' Grid.Row='0' >Name:</TextBlock>

    <TextBlock Grid.Column='0' Grid.Row='1' >Address</TextBlock>

 

    <TextBox Grid.Column='1' Grid.Row='0' Name='NameField' />

    <TextBox Grid.Column='1' Grid.Row='1' Name='AddressField' />

 

  </Grid>

</Window>

... which looks like this:

image

Now add some help text.  I'll add specific help text to the NameField text box, and general help text to the Window, but nothing to the AddressField, just to show how that behaves:

<Window x:Class="ContextSensitiveHelp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300"

    xmlns:h="clr-namespace:ContextSensitiveHelp"

    h:HelpProvider.HelpString='This form is for your name and address'  >

 

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width='auto'/>

      <ColumnDefinition Width='*'/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height='auto'/>

      <RowDefinition Height='auto' />

    </Grid.RowDefinitions>

 

    <TextBlock Grid.Column='0' Grid.Row='0' >Name:</TextBlock>

    <TextBlock Grid.Column='0' Grid.Row='1' >Address</TextBlock>

 

    <TextBox Grid.Column='1' Grid.Row='0' Name='NameField'

               h:HelpProvider.HelpString="Enter your name here"/>

    <TextBox Grid.Column='1' Grid.Row='1' Name='AddressField' />

 

  </Grid>

</Window>

So you see here that the help text is being provided by attached properties defined by a class named HelpProvider.  Here's the attached property definition:

static class HelpProvider

{

    public static string GetHelpString(DependencyObject obj)

    {

        return (string)obj.GetValue(HelpStringProperty);

    }

 

    public static void SetHelpString(DependencyObject obj, string value)

    {

        obj.SetValue(HelpStringProperty, value);

    }

 

    public static readonly DependencyProperty HelpStringProperty =

        DependencyProperty.RegisterAttached(

                       "HelpString", typeof(string), typeof(HelpProvider));

}

I made this a help string property, but you could have a HelpKeyword property that held a help topic ID, or a HelpURI property that referenced online documentation, etc.

Now we just need to respond to the command.  The way you respond to commands is with event listeners registered to handle the command.  You can simply set up a command handler on a particular element by setting a CommandBinding like this:

<Window x:Class="ContextSensitiveHelp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300"

    xmlns:h="clr-namespace:ContextSensitiveHelp"

    h:HelpProvider.HelpString='This form is for your name and address'  >

 

  <Window.CommandBindings>

    <CommandBinding Command='ApplicationCommands.Help' Executed='Window_Help_Executed' />

  </Window.CommandBindings>

 

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width='auto'/>

      <ColumnDefinition Width='*'/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height='auto'/>

      <RowDefinition Height='auto' />

    </Grid.RowDefinitions>

 

    <TextBlock Grid.Column='0' Grid.Row='0' >Name:</TextBlock>

    <TextBlock Grid.Column='0' Grid.Row='1' >Address</TextBlock>

 

    <TextBox Grid.Column='1' Grid.Row='0' Name='NameField'

               h:HelpProvider.HelpString="Enter your name here"/>

    <TextBox Grid.Column='1' Grid.Row='1' Name='AddressField' />

 

  </Grid>

</Window>

... and then respond to the command like this:

private void Window_Help_Executed(object sender, ExecutedRoutedEventArgs e)

{

    FrameworkElement source = e.Source as FrameworkElement;

    if (source != null)

    {

        string helpString = HelpProvider.GetHelpString(source);

        if (helpString != null)

        {

            System.Windows.MessageBox.Show("Help: " + helpString);

        }

    }

}

The problem with that is that you'd really rather not implement the handling as part of your application, but rather as part of the HelpProvider (following the WinForms model).  Also, the code above finds the HelpString on NameField, which has a HelpString set, but it doesn't find anything for the AddressField, because it doesn't have a good way to find the more general HelpString set on the Window.

So instead of putting the command handler in the Window, we'll put it in the HelpProvider itself.  We can do this by registering a command handler directly against the FrameworkElement class; now we'll get called for any FrameworkElement instance in the application.  We'll also register for the command's CanExecute handler.  This allows us to decide if we should handle a command at an element, or let it bubble up to a higher handler.  That is, this will allow the Help command to bubble up from AddressField, which has no HelpString, to Window which does.

The HelpProvider class is then updated with these new members:

static class HelpProvider

{

    static HelpProvider()

    {

        CommandManager.RegisterClassCommandBinding(

            typeof(FrameworkElement),

            new CommandBinding(

                ApplicationCommands.Help,

                new ExecutedRoutedEventHandler(Executed),

                new CanExecuteRoutedEventHandler(CanExecute)));

    }

 

    static private void CanExecute(object sender, CanExecuteRoutedEventArgs e)

    {

 

        FrameworkElement senderElement = sender as FrameworkElement;

 

        if (HelpProvider.GetHelpString(senderElement) != null)

            e.CanExecute = true;

    }

 

    static private void Executed(object sender, ExecutedRoutedEventArgs e)

    {

        MessageBox.Show("Help: " + HelpProvider.GetHelpString(sender as FrameworkElement));

    }

 

    ...

... and we can remove the CommandBindings from the Window:

<Window x:Class="ContextSensitiveHelp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300"

    xmlns:h="clr-namespace:ContextSensitiveHelp"

    h:HelpProvider.HelpString='This form is for your name and address'  >

 

  <Window.CommandBindings>

    <CommandBinding Command='ApplicationCommands.Help' Executed='Window_Help_Executed' />

  </Window.CommandBindings>

 

  <Grid>

    <Grid.ColumnDefinitions>

      <ColumnDefinition Width='auto'/>

      <ColumnDefinition Width='*'/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

      <RowDefinition Height='auto'/>

      <RowDefinition Height='auto' />

    </Grid.RowDefinitions>

 

    <TextBlock Grid.Column='0' Grid.Row='0' >Name:</TextBlock>

    <TextBlock Grid.Column='0' Grid.Row='1' >Address</TextBlock>

 

    <TextBox Grid.Column='1' Grid.Row='0' Name='NameField'

               h:HelpProvider.HelpString="Enter your name here"/>

    <TextBox Grid.Column='1' Grid.Row='1' Name='AddressField' />

 

  </Grid>

</Window>

... and now F1 on NameField displays "Enter your name here", and F1 on the AddressField displays "This form is for your name and address".

Finally, note that you can also do more exciting things in the help handler, like load a chm file with the System.Windows.Forms.Help class:

static private void Executed(object sender, ExecutedRoutedEventArgs e)

{

    System.Windows.Forms.Help.ShowHelp(null, @"MyHelp.chm");

}

 

Published Thursday, July 26, 2007 10:56 PM by MikeHillberg

Comments

# WPF Articles @ Sunday, July 29, 2007 10:28 AM

Dr Tim Sneath gives a overview of what is new in WPF 3.5 http://blogs.msdn.com/tims/archive/2007/07/27

Rudi Grobler

# re: A Context-Sensitive Help Provider in Wpf @ Tuesday, August 14, 2007 3:32 AM

Any ideas on how to make this appear based on mouseover and not just focus?

Attempting to create office 2007 style super tooltips with the F1 for more help.

Andreas

andemann

# re: A Context-Sensitive Help Provider in Wpf @ Wednesday, August 15, 2007 3:17 AM

I found the answer. Use CommandBinding in the custom toolTip contructor.

Andreas

andemann

# re: A Context-Sensitive Help Provider in Wpf @ Friday, August 31, 2007 5:21 AM

I've created a full code for HelpProvider:

using System.Windows;

using System.Windows.Forms;

using System.Windows.Input;

using System.Windows.Media;

namespace Diginesis.Presentation.Controls

{

   public static class DiginesisHelpProvider

   {

       #region Fields

       private static string mHelpNamespace = null;

       private static bool mShowHelp = false;

       private static string mNotFoundTopic;

       public static readonly DependencyProperty HelpKeywordProperty =

           DependencyProperty.RegisterAttached("HelpKeyword", typeof (string), typeof (DiginesisHelpProvider));

       public static readonly DependencyProperty HelpNavigatorProperty =

           DependencyProperty.RegisterAttached("HelpNavigator", typeof (HelpNavigator), typeof (DiginesisHelpProvider),

                                               new PropertyMetadata(HelpNavigator.TableOfContents));

       public static readonly DependencyProperty ShowHelpProperty =

           DependencyProperty.RegisterAttached("ShowHelp", typeof (bool), typeof (DiginesisHelpProvider));

       #endregion

       #region Constructors

       static DiginesisHelpProvider()

       {

           CommandManager.RegisterClassCommandBinding(

               typeof (FrameworkElement),

               new CommandBinding(ApplicationCommands.Help, OnHelpExecuted, OnHelpCanExecute));

       }

       #endregion

       #region Public Properties

       public static string HelpNamespace

       {

           get { return mHelpNamespace; }

           set { mHelpNamespace = value; }

       }

       public static bool ShowHelp

       {

           get { return mShowHelp; }

           set { mShowHelp = value; }

       }

       public static string NotFoundTopic

       {

           get { return mNotFoundTopic; }

           set { mNotFoundTopic = value; }

       }

       #endregion

       #region Public Methods

       #region HelpKeyword

       public static string GetHelpKeyword(DependencyObject obj)

       {

           return (string) obj.GetValue(HelpKeywordProperty);

       }

       public static void SetHelpKeyword(DependencyObject obj, string value)

       {

           obj.SetValue(HelpKeywordProperty, value);

       }

       #endregion

       #region HelpNavigator

       public static HelpNavigator GetHelpNavigator(DependencyObject obj)

       {

           return (HelpNavigator) obj.GetValue(HelpNavigatorProperty);

       }

       public static void SetHelpNavigator(DependencyObject obj, HelpNavigator value)

       {

           obj.SetValue(HelpNavigatorProperty, value);

       }

       #endregion

       #region ShowHelp

       public static bool GetShowHelp(DependencyObject obj)

       {

           return (bool) obj.GetValue(ShowHelpProperty);

       }

       public static void SetShowHelp(DependencyObject obj, bool value)

       {

           obj.SetValue(ShowHelpProperty, value);

       }

       #endregion

       #endregion

       #region Private Members

       private static void OnHelpCanExecute(object sender, CanExecuteRoutedEventArgs e)

       {

           e.CanExecute = CanExecuteHelp((DependencyObject) sender) || ShowHelp;

       }

       private static bool CanExecuteHelp(DependencyObject sender)

       {

           if (sender != null)

           {

               if (GetShowHelp(sender))

                   return true;

               return CanExecuteHelp(VisualTreeHelper.GetParent(sender));

           }

           return false;

       }

       private static DependencyObject GetHelp(DependencyObject sender)

       {

           if (sender != null)

           {

               if (GetShowHelp(sender))

                   return sender;

               return GetHelp(VisualTreeHelper.GetParent(sender));

           }

           return null;

       }

       private static void OnHelpExecuted(object sender, ExecutedRoutedEventArgs e)

       {

           DependencyObject ctl = GetHelp(sender as DependencyObject);

           if (ctl != null && GetShowHelp(ctl))

           {

               string parameter = GetHelpKeyword(ctl);

               HelpNavigator command = GetHelpNavigator(ctl);

               if (!e.Handled && !string.IsNullOrEmpty(HelpNamespace))

               {

                   if (!string.IsNullOrEmpty(parameter))

                   {

                       Help.ShowHelp(null, HelpNamespace, command, parameter);

                       e.Handled = true;

                   }

                   if (!e.Handled)

                   {

                       Help.ShowHelp(null, HelpNamespace, command);

                       e.Handled = true;

                   }

               }

               if (!e.Handled && !string.IsNullOrEmpty(HelpNamespace))

               {

                   Help.ShowHelp(null, HelpNamespace);

                   e.Handled = true;

               }

           }

           else if (ShowHelp)

           {

               if (!string.IsNullOrEmpty(NotFoundTopic))

                   Help.ShowHelp(null, HelpNamespace, NotFoundTopic);

               else

                   Help.ShowHelp(null, HelpNamespace);

               e.Handled = true;

           }

       }

       #endregion

   }

}

black_nm

# re: A Context-Sensitive Help Provider in Wpf @ Friday, August 31, 2007 5:24 AM

How to use it!?!?!

Add this to the App startup or constructor

DiginesisHelpProvider.HelpNamespace = "Help.chm";

           DiginesisHelpProvider.ShowHelp = true;

And set on a control the properties:

c:DiginesisHelpProvider.ShowHelp="True"

                  c:DiginesisHelpProvider.HelpKeyword="HelpTest2.htm"

                  c:DiginesisHelpProvider.HelpNavigator="Topic"

black_nm

# WPF/Silverlight/XAML Web Links - 2004/04/18 @ Friday, April 18, 2008 11:35 PM

&#160; WPF Apps Jaime Rodriguez&#39;s list of WPF in Line-of-Business case studies CodePlex Project:

Rob Relyea - Xamlified

# WPF/Silverlight/XAML Web Links - 2008/04/18 @ Saturday, April 19, 2008 12:01 AM

WPF Apps Jaime Rodriguez&#39;s list of WPF in Line-of-Business case studies CodePlex Project: Slick Code

Rob Relyea - Xamlified

# clr-namespace:ContextSensitiveHelp not available @ Wednesday, May 28, 2008 8:24 AM

clr-namespace:ContextSensitiveHelp not available in my WPF project.

Do I need to added some reference assembly?

kyawtun4biz

# re: clr-namespace:ContextSensitiveHelp not available @ Thursday, May 29, 2008 8:06 PM

Sorry, ContextSensitiveHelp is the namespace that the HelpProvider class lives in.  E.g.

namespace ContextSensitiveHelp

{

   static class HelpProvider

   {

       ...

   }

}

MikeHillberg

# How to provide a context sentitive help to your WPF application @ Tuesday, June 10, 2008 8:17 AM

I found thie following interesting post where is well described how to provide your windows presentation

NicolBlog

Anonymous comments are disabled
Page view tracker