- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用具有键盘快捷键的菜单项来创建可本地化的WPF菜单栏-而不是加速键/助记符(通常显示为带下划线的字符,可以在菜单已打开时按下以直接选择菜单项),但是键盘快捷键(通常是Ctrl +另一个键的组合),显示在菜单项标题旁边右对齐。
我正在为我的应用程序使用MVVM模式,这意味着我避免在可能的情况下将任何代码放在代码的后面,并让我的 View 模型(我分配给 DataContext
properties)提供 ICommand
interface的实现,这些实现由 View 中的控件使用。
作为重现此问题的基础,这里是一些如上所述的应用程序最小源代码:
Window1.xaml
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}"/>
</MenuItem>
</Menu>
</Window>
using System;
using System.Windows;
namespace MenuShortcutTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}
using System;
using System.Windows;
using System.Windows.Input;
namespace MenuShortcutTest
{
public class MainViewModel
{
public string MenuHeader {
get {
// in real code: load this string from localization
return "Menu";
}
}
public string DoSomethingHeader {
get {
// in real code: load this string from localization
return "Do Something";
}
}
private class DoSomethingCommand : ICommand
{
public DoSomethingCommand(MainViewModel owner)
{
if (owner == null) {
throw new ArgumentNullException("owner");
}
this.owner = owner;
}
private readonly MainViewModel owner;
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
// in real code: do something meaningful with the view-model
MessageBox.Show(owner.GetType().FullName);
}
public bool CanExecute(object parameter)
{
return true;
}
}
private ICommand doSomething;
public ICommand DoSomething {
get {
if (doSomething == null) {
doSomething = new DoSomethingCommand(this);
}
return doSomething;
}
}
}
}
MenuItem
class具有
InputGestureText
property,但是如SO问题(如
this,
this,
this和
this)中所述,它纯粹是修饰性的,对应用程序实际处理的快捷方式没有任何影响。
KeyBinding
列表中的
InputBindings
链接。在启用该功能的同时,它不会自动显示带有菜单项的快捷方式。
Window1.xaml 更改如下:
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{Binding DoSomething}"/>
</Window.InputBindings>
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}"/>
</MenuItem>
</Menu>
</Window>
InputGestureText
属性,使
Window1.xaml 看起来像这样:
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuShortcutTest" Height="300" Width="300">
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{Binding DoSomething}"/>
</Window.InputBindings>
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{Binding DoSomething}" InputGestureText="Ctrl+D"/>
</MenuItem>
</Menu>
</Window>
IValueConverter
来将
InputGestureText
属性绑定(bind)到窗口的
InputBindings
列表(在
KeyBinding
列表中可能有多个
InputBindings
,或者根本没有,所以我没有可以绑定(bind)的特定
KeyBinding
实例到(如果
KeyBinding
甚至使其成为绑定(bind)目标))。在我看来,这是最理想的解决方案,因为它非常灵活并且同时非常干净(它不需要在各个地方进行过多的声明),但是一方面,
InputBindingCollection
并未实现
INotifyCollectionChanged
,因此当替换快捷方式时,绑定(bind)将不会更新,另一方面,我没有以整洁的方式为转换器提供对我的 View 模型的引用(它需要访问本地化数据)。而且,
InputBindings
不是依赖项属性,因此我也无法将其绑定(bind)到
ItemGestureText
属性也可以绑定(bind)到的公共(public)源(例如位于 View 模型中的输入绑定(bind)列表)。
RoutedCommand
和
RoutedUICommand
包含内置的
InputGestures
property,这意味着来自该属性的键绑定(bind)会自动显示在菜单项中。
ICommand
实现似乎打开了一个新的蠕虫 jar ,因为它们的
Execute
和
CanExecute
方法不是虚拟的,因此不能在子类中覆盖以填充所需的功能。提供此功能的唯一方法似乎是在XAML中声明一个
CommandBinding
(例如
here或
here所示),该命令将命令与事件处理程序连接-但是,该事件处理程序将位于后面的代码中,从而违反了MVVM体系结构如上所述。
<Window x:Class="MenuShortcutTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MenuShortcutTest"
Title="MenuShortcutTest" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:DoSomethingCommand.Instance}" Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<Menu>
<MenuItem Header="{Binding MenuHeader}">
<MenuItem Header="{Binding DoSomethingHeader}" Command="{x:Static local:DoSomethingCommand.Instance}"/>
</MenuItem>
</Menu>
</Window>
using System;
using System.Windows;
namespace MenuShortcutTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
((MainViewModel)DataContext).DoSomething();
}
}
}
using System;
using System.Windows;
using System.Windows.Input;
namespace MenuShortcutTest
{
public class MainViewModel
{
public string MenuHeader {
get {
// in real code: load this string from localization
return "Menu";
}
}
public string DoSomethingHeader {
get {
// in real code: load this string from localization
return "Do Something";
}
}
public void DoSomething()
{
// in real code: do something meaningful with the view-model
MessageBox.Show(this.GetType().FullName);
}
}
}
using System;
using System.Windows.Input;
namespace MenuShortcutTest
{
public class DoSomethingCommand : RoutedCommand
{
public DoSomethingCommand()
{
this.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
}
private static Lazy<DoSomethingCommand> instance = new Lazy<DoSomethingCommand>();
public static DoSomethingCommand Instance {
get {
return instance.Value;
}
}
}
}
RoutedCommand.Execute
等是非虚拟的),我不知道如何像创建基于
RoutedCommand
的
in an answer to this question一样使用
RelayCommand
来对
RoutedCommand
进行子类化,因此我不必绕过
InputBindings
窗口的外观-在
ICommand
子类中显式重新实现
RoutedCommand
的方法时,感觉像是在破坏某些东西。
RoutedCommand
中配置的此方法自动显示的,但它似乎并未自动本地化。我的理解是
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-de");
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
MainWindow
构造函数的过程中,应确保框架提供的可本地化字符串应取自德语
CultureInfo
-但是,
Ctrl
不会更改为
Strg
,因此,除非我对如何为框架提供的字符串设置
CultureInfo
感到误解,此方法无论如何,如果我希望所显示的快捷方式已正确本地化,则是不可行的。
KeyGesture
允许我为键盘快捷键指定一个自定义显示字符串,但不仅
RoutedCommand
派生的
DoSomethingCommand
类与我的所有实例(从那里我可以与加载的本地化联系)脱节。与
CommandBinding
必须与XAML中的命令链接的方式相同,
respective DisplayString
property是只读的,因此当在运行时加载另一个本地化时将无法更改它。
InputBindings
列表以检查哪个命令具有与之关联的任何
KeyBinding
实例,并且哪些菜单项链接至这些命令中的任何一个,因此我可以手动设置各个菜单项中每个菜单项的
InputGestureText
,以反射(reflect)第一个(或首选,以我想采用的任何度量标准)使用此处)键盘快捷键。每当我认为键绑定(bind)可能已更改时,都必须重复此过程。但是,对于某些本质上似乎是菜单栏GUI的基本功能的东西来说,这似乎是一个非常繁琐的解决方法,因此我坚信这不是实现此目的的“正确”方法。
MenuItem
实例的键盘快捷键的正确方法是什么?
KeyBinding
/
KeyGesture
实际启用
InputGestureText
可视化暗示的功能,而没有说明如何在所述情况下自动链接这两个方面。我发现的唯一一个有希望的问题是
this,但是两年多来没有收到任何答复。
最佳答案
我将从警告开始。有可能您不仅需要可自定义的热键,还需要菜单本身。因此,在静态使用InputBindings
之前,请三思。
还有一种关于InputBindings
的警告:它们暗示命令与窗口的可视树中的元素相关。有时您需要不与任何特定窗口连接的全局热键。
上面所说的意味着您可以使用另一种方法,并通过正确路由到相应的命令来实现自己的应用程序范围的手势处理(不要忘记使用对命令的弱引用)。
尽管如此,手势感知命令的思想是相同的。
public class CommandWithHotkey : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBox.Show("It Worked!");
}
public KeyGesture Gesture { get; set; }
public string GestureText
{
get { return Gesture.GetDisplayStringForCulture(CultureInfo.CurrentUICulture); }
}
public string Text { get; set; }
public event EventHandler CanExecuteChanged;
public CommandWithHotkey()
{
Text = "Execute Me";
Gesture = new KeyGesture(Key.K, ModifierKeys.Control);
}
}
public class ViewModel
{
public ICommand Command { get; set; }
public ViewModel()
{
Command = new CommandWithHotkey();
}
}
<Window x:Class="CommandsWithHotKeys.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:commandsWithHotKeys="clr-namespace:CommandsWithHotKeys"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<commandsWithHotKeys:ViewModel/>
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Command="{Binding Command}" Key ="{Binding Command.Gesture.Key}" Modifiers="{Binding Command.Gesture.Modifiers}"></KeyBinding>
</Window.InputBindings>
<Grid>
<Menu HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="Auto">
<MenuItem Header="Test">
<MenuItem InputGestureText="{Binding Command.GestureText}" Header="{Binding Command.Text}" Command="{Binding Command}">
</MenuItem>
</MenuItem>
</Menu>
</Grid>
</Window>
关于wpf - 如何显示菜单项的工作键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21972243/
有没有办法在另一个 WPF 窗口内托管 WPF 窗口。我有几个有点复杂的表格。但现在为了简化事情,我试图将其中一些合并为一个“仪表板”表单中的标签页。 请注意,我不是要托管 Windows 窗体,而是
WPF 特有的哪些方面和实践在非 WPF GUI 编程中最有用(并且并非难以实现)? 最佳答案 通过学习 WPF 命令,我了解了命令模式。它构成了 UI - 代码分离的基础,我认为应该在其他应用程序中
WinRT/Metro 正在获得一个新的 SemanticZoom控制,但我很难为 WPF 找到任何东西。 我不想为我的特定项目切换到 Metro,因为它不允许我制作窗口应用程序或跨多个显示器的多个实
我很难解决我的问题,我快要疯了。 想法是这样的:我有两个 ListView 元素,当一个元素从第一个列表掉落到第二个列表时,我需要打开一个对话,但我需要被掉落的元素的信息以及被添加以填充对话的元素。
如果我遵循TabControl,并且一切正常,当我切换到第二个Tabitem时,它显示就没有问题。 //datagrid //datagrid2 但是如果我有这个xaml,当我
在 Windows 窗体应用程序中,我们的数据 GridView 有很多事件,如行鼠标双击或行单击以及额外的...... 但是在 WPF 中我找不到这些事件。我如何将行鼠标双击添加到其中包含数据网格的
在这个项目中,代码 正确编译和执行 ;但是,我需要帮助解决两个问题: VS2012 WPF 设计器不适用于此 XAML 文件。它显示消息设计 View 对于 x64 和 ARM 目标平台不可用。 我收
目前我正在设计 WPF ScrollViewer,我发现了这个 Content="M 0 0 L 4 4 L 0 8 Z" 阅读 MSDN examples .现在我真的很想知道这意味着什么,但我无法
在 WPF 中,元素的可见性可以为“可见”,但实际上在屏幕上不可见,因为它的父元素(或父元素的父元素)具有折叠的可见性。 我希望能够知道一个元素是否实际呈现在屏幕上,而不必遍历可视化树检查父元素。 有
我应该使用 ApplicationCommands.Close用于关闭模式对话框还是该命令被认为是为关闭应用程序保留的?如果是后者,请大家创建Close每个命令 Dialog盒子或只是一个 Close
WPF 是否有任何可用的 piemenu 控件? 最佳答案 我在我的最爱中找到了这个,你可以看看: This 祝你今天过得愉快。 关于wpf - WPF 的菜单,我们在Stack Overflow上找
我正在尝试使用 WrapPanel 和两个 TextBlock 将星号 (*) 附加到某些文本的左侧,允许文本换行,并强制文本右对齐。通过创建一个 FlowDirection 设置为 RightToL
这里是场景(简化):我在Window上有一个控件(比如说一个Rectangle)。我迷上了MouseMove事件,以使其启动拖放操作。然后在MouseDown事件中进行动画处理,向右移动50个像素。但
我有一个 ListView ,它的项目来源是一个列表。我希望用户只选择一项。当我将 listview 的 SelectionMode 设置为 single 时,用户仍然可以选择多个项目,并且似乎 li
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
INotifyPropertyChanged 的目的是什么。我知道每当更改属性时都会触发此事件,但是 View /用户界面如何知道触发了此事件: 这是实现 INotifyPropertyChang
我正在查看工具箱中的 WPF 组件,但找不到 2005/2008 中存在的错误提供程序。 被移除了吗? 最佳答案 ErrorProvider是一个 Winforms 控件。 WPF 中没有等效项。但是
我试图在单击和双击 wpf Image 控件时有不同的行为。不幸的是,单击首先被触发,因此双击被忽略。 最佳答案 如果您改用 MouseDown 事件,则它在 EventArgs 中为 ClickCo
这可能吗? 我使用了一个框架控件并且:显示(例如:showwindow.xaml) 但是我得到这个错误: root element is not valid for navigation 最佳答案 确
我在蓝色背景的窗口上放置了一个扩展器,我想让扩展器的按钮与默认颜色不同(蓝色,它是从窗口接收的)。当我修改扩展器的背景属性时,它会将整个扩展器、标题和全部更改为新颜色。但是,我只想更改按钮本身。谁能指
我是一名优秀的程序员,十分优秀!