【发布时间】:2020-06-05 11:33:50
【问题描述】:
我想在按下 SHIFT 键时更改按钮的文本(内容属性)。在这种情况下,按钮应执行不同的命令。这是一种常见的 UI 行为 e。 G。在 Photoshop 中。
任何想法如何做到这一点。
在此先感谢
【问题讨论】:
我想在按下 SHIFT 键时更改按钮的文本(内容属性)。在这种情况下,按钮应执行不同的命令。这是一种常见的 UI 行为 e。 G。在 Photoshop 中。
任何想法如何做到这一点。
在此先感谢
【问题讨论】:
将KeyDown 或PreviewKeyDown 事件添加到您的Button 元素。
<Button Width="300" Height="50" Name="btnFunction" KeyDown="btnFunctionKeyDown" Content="Function1"/>
还有 C# 代码:
private void btnFunctionKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
btnFunction.Content = "Function2";
}
}
查看这篇文章了解更多信息:
https://docs.microsoft.com/de-de/dotnet/api/system.windows.input.keyboard.keydown?view=netcore-3.1
【讨论】:
这是我的解决方案(事件在窗口处理)-非常感谢您的输入-如果有更好的解决方案,请评论...
internal void HandlePreviewKeyDown(KeyEventArgs e)
{
IInputElement focusedControl = FocusManager.GetFocusedElement(_window);
if (( (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) && !(focusedControl?.GetType() == typeof(TextBox)))
{
// set button text
e.Handled = true;
}
}
internal void HandlePreviewKeyUp(KeyEventArgs e)
{
IInputElement focusedControl = FocusManager.GetFocusedElement(_window);
if ( (e.Key == Key.LeftShift) || (e.Key == Key.RightShift) && !(focusedControl?.GetType() == typeof(TextBox)))
{
// re-set button text
e.Handled = true;
}
}
【讨论】: