我的菜单快捷方式都没有效果。
我已经在这个问题上阅读了一些内容,根据答案here,这意味着我需要使用命令而不是点击。
这是菜单结构:
<MenuItem x:Uid="MenuItem_10" Header="Zoom In" InputGestureText="CTRL +" Click="menuViewZoomInOutScaleFactor" Tag="25">
<MenuItem.InputBindings>
<KeyBinding x:Uid="KeyBinding_7" Key="OemPlus" Modifiers="Ctrl" />
</MenuItem.InputBindings>
</MenuItem>
<MenuItem x:Uid="MenuItem_11" Header="Zoom Out" InputGestureText="CTRL -" Click="menuViewZoomInOutScaleFactor" Tag="-25">
<MenuItem.InputBindings>
<KeyBinding x:Uid="KeyBinding_8" Key="OemMinus" Modifiers="Ctrl"/>
</MenuItem.InputBindings>
</MenuItem>
<Separator x:Uid="Separator_7"/>
<MenuItem x:Uid="MenuItem_12" Header="400%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=400}, Mode=OneWay}" Tag="400" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_13" Header="300%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=300}, Mode=OneWay}" Tag="300" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_14" Header="250%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=250}, Mode=OneWay}" Tag="250" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_15" Header="200%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=200}, Mode=OneWay}" Tag="200" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_16" Header="175%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=175}, Mode=OneWay}" Tag="175" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_17" Header="150%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=150}, Mode=OneWay}" Tag="150" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_18" Header="125%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=125}, Mode=OneWay}" Tag="125" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_19" Header="100%" IsCheckable="True" InputGestureText="CTRL + 0" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=100}, Mode=OneWay}" Tag="100" Click="menuViewZoomScaleFactor">
<MenuItem.InputBindings>
<KeyBinding x:Uid="KeyBinding_9" Key="D0" Modifiers="Ctrl"/>
</MenuItem.InputBindings>
</MenuItem>
<MenuItem x:Uid="MenuItem_20" Header="75%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=75}, Mode=OneWay}" Tag="75" Click="menuViewZoomScaleFactor"/>
<MenuItem x:Uid="MenuItem_21" Header="50%" IsCheckable="True" IsChecked="{Binding Source={x:Static Properties:Settings.Default}, Path=ZoomFactor, Converter={ValueConverters:IsIntegerEqual To=50}, Mode=OneWay}" Tag="50" Click="menuViewZoomScaleFactor"/>
<Separator x:Uid="Separator_8"/>
<MenuItem x:Uid="MenuItem_22" Header="Custom..."/>
如您所见,有三个菜单项与:
相关联我无法在密钥列表中找到这些特定字母,并已在D0
,OemPlus
和OemMinus
中找到。无论如何,我的所有键绑定都没有被实际处理。为什么呢?
我无法看到如何将点击转换为命令。到目前为止,我的所有命令都与视图模型捆绑在一起。但在这种情况下,我需要将命令与后面的代码相关联。
以下是我当前的点击处理程序:
private void menuViewZoomScaleFactor(object sender, RoutedEventArgs e)
{
SetZoomFactor(Int32.Parse((String)((MenuItem)sender).Tag));
}
private void menuViewZoomInOutScaleFactor(object sender, RoutedEventArgs e)
{
int iStep = Int32.Parse((String)((MenuItem)sender).Tag);
if ((iStep == 25 && Settings.Default.ZoomFactor <= 375) ||
(iStep == -25 && Settings.Default.ZoomFactor >= 75))
{
SetZoomFactor(Settings.Default.ZoomFactor + iStep);
}
}
private void SetZoomFactor(int iZoomFactor)
{
Settings.Default.ZoomFactor = iZoomFactor;
IServiceProvider serviceProvider = myWorkbookView.Document as IServiceProvider;
Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
Guid serviceGuid = SID_SWebBrowserApp;
Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
SHDocVw.IWebBrowser2 WebBrowser = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
WebBrowser.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Settings.Default.ZoomFactor);
}
所以我必须让我的菜单键绑定功能正常。
答案 0 :(得分:2)
如果要在WPF中使用InputBindings
,则必须使用命令。您需要为菜单项分配命令以及添加命令绑定和输入手势。您可以使用CommandParameter
将参数传递给命令绑定处理程序。
将路由命令对象视为键,它定义命令源(菜单项或输入绑定)与命令绑定之间的链接。命令绑定定义可以运行命令的可视树的范围。
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow">
<Window.CommandBindings>
<CommandBinding Command="IncreaseZoom"
Executed="IncreaseZoom_OnExecuted" />
<CommandBinding Command="Zoom"
Executed="Zoom_OnExecuted" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+Plus"
Command="IncreaseZoom" />
</Window.InputBindings>
<Grid>
<Menu>
<MenuItem Header="View">
<MenuItem x:Uid="MenuItem_10"
Header="Zoom In"
InputGestureText="CTRL +"
Command="IncreaseZoom" />
<MenuItem x:Uid="MenuItem_12"
Header="400%"
IsCheckable="True"
IsChecked="{Binding Whatever}"
Command="Zoom">
<MenuItem.CommandParameter>
<system:Int32>400</system:Int32>
</MenuItem.CommandParameter>
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
代码背后:
private void IncreaseZoom_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
IncrementZoom(25);
}
private void Zoom_OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
var zoom = (int)e.Parameter;
SetZoom(zoom);
}
如果在内置命令中找不到合适的命令,可以在静态类中定义自己的命令:
public static class MyCommands
{
public static readonly RoutedCommand SampleCommand = new RoutedCommand(
nameof(SampleCommand), typeof(MyCommands),
new InputGestureCollection { new KeyGesture(Key.B, ModifierKeys.Alt) });
}
如您所见,您甚至可以定义默认输入手势,因此您无需在XAML中指定其他输入绑定(只需命令绑定)。要在XAML中使用此类:
<MenuItem Command="{x:Static m:MyCommands.SampleCommand}" />