Context-Sensitive Help Provider in Wpf

http://blogs.msdn.com/b/mikehillberg/archive/2007/07/26/a-context-sensitive-help-provider-in-wpf.aspx

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.
1)给主 窗口显示Help信息。

public MainWindow()
        {
            InitializeComponent();
            CommandBinding helpBinding = new CommandBinding(ApplicationCommands.Help);
            helpBinding.CanExecute += (sender, e) => e.CanExecute = true;
            helpBinding.Executed += (sender, e) =>
                                        {
                                            var dir = new DirectoryInfo(ExplorerContent.GetCurrentAssemblyPath());
                                            dir = dir.Parent;
                                            var helpFile = Path.Combine(dir.FullName, @"*.chm");
                                            if (File.Exists(helpFile))
                                            {
                                                System.Windows.Forms.Help.ShowHelp(null, helpFile);
                                            }
                                        };
            CommandBindings.Add(helpBinding);
}
}

2)提供一个HelpProvider,让窗口和各个空间可以显示自己的Help信息。

<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".


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)
        {
            //System.Windows.Forms.Help.ShowHelp(null, @"C:\Program Files (x86)\DNVS\Nauticus Machinery 11.0 (2012) Client\Config\Manuals\Nauticus Machinery Help.chm");
            System.Windows.MessageBox.Show("Help: " + HelpProvider.GetHelpString(sender as FrameworkElement));
            //Help.ShowHelp(null, @"E:\Windows\IME\imekr8\help\imkr.chm");
        }



        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));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值