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));
}