【发布时间】:2013-08-27 09:06:53
【问题描述】:
我有一个RoutedUICommand 命令,可以通过两种不同的方式触发:
- 在按钮点击事件时直接通过
ICommand.Execute; - 使用声明性语法:
<button Command="local:MainWindow.MyCommand" .../>。
该命令仅由顶部窗口处理:
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.MyCommand" CanExecute="CanExecuteCommmand" Executed="CommandExecuted"/>
</Window.CommandBindings>
第一种方法只有在窗口中有焦点元素时才有效。无论焦点如何,第二个总是如此。
我查看了 BCL 的 ICommand.Execute 实现,发现如果 Keyboard.FocusedElement 是 null,则不会触发该命令,因此这是设计使然。我仍然会对此提出疑问,因为即使应用程序没有 UI 焦点(例如,我可能想调用 @ 987654331@ 收到套接字消息时来自异步任务)。就这样吧,我仍然不清楚为什么第二种(声明式)方法总是不管焦点状态如何都有效。
我对 WPF 命令路由的理解缺少什么?我确信这“不是错误,而是一项功能”。
下面是代码。如果你喜欢玩它,这里是full project。单击第一个按钮 - 该命令将被执行,因为焦点在 TextBox 内。单击第二个按钮 - 一切都很好。单击Clear Focus 按钮。现在第一个按钮 (ICommand.Execute) 不执行命令,而第二个按钮仍然执行。您需要点击TextBox 以使第一个按钮再次起作用,因此有一个焦点元素。
这是一个人为的例子,但它具有现实生活的影响。我将发布一个有关使用WindowsFormsHost ([EDITED] asked here) 托管 WinForms 控件的相关问题,在这种情况下,Keyboard.FocusedElement 始终是 null,当焦点位于 @ 内部时987654339@(通过ICommand.Execute有效杀死命令执行)。
XAML 代码:
<Window x:Class="WpfCommandTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCommandTest"
Title="MainWindow" Height="480" Width="640" Background="Gray">
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.MyCommand" CanExecute="CanExecuteCommmand" Executed="CommandExecuted"/>
</Window.CommandBindings>
<StackPanel Margin="20,20,20,20">
<TextBox Name="textBoxOutput" Focusable="True" IsTabStop="True" Height="300"/>
<Button FocusManager.IsFocusScope="True" Name="btnTest" Focusable="False" IsTabStop="False" Content="Test (ICommand.Execute)" Click="btnTest_Click" Width="200"/>
<Button FocusManager.IsFocusScope="True" Focusable="False" IsTabStop="False" Content="Test (Command property)" Command="local:MainWindow.MyCommand" Width="200"/>
<Button FocusManager.IsFocusScope="True" Name="btnClearFocus" Focusable="False" IsTabStop="False" Content="Clear Focus" Click="btnClearFocus_Click" Width="200" Margin="138,0,139,0"/>
</StackPanel>
</Window>
C#代码,大部分与焦点状态记录有关:
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfCommandTest
{
public partial class MainWindow : Window
{
public static readonly RoutedUICommand MyCommand = new RoutedUICommand("MyCommand", "MyCommand", typeof(MainWindow));
const string Null = "null";
public MainWindow()
{
InitializeComponent();
this.Loaded += (s, e) => textBoxOutput.Focus(); // set focus on the TextBox
}
void CanExecuteCommmand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
var routedCommand = e.Command as RoutedCommand;
var commandName = routedCommand != null ? routedCommand.Name : Null;
Log("*** Executed: {0} ***, {1}", commandName, FormatFocus());
}
void btnTest_Click(object sender, RoutedEventArgs e)
{
Log("btnTest_Click, {0}", FormatFocus());
ICommand command = MyCommand;
if (command.CanExecute(null))
command.Execute(null);
}
void btnClearFocus_Click(object sender, RoutedEventArgs e)
{
FocusManager.SetFocusedElement(this, this);
Keyboard.ClearFocus();
Log("btnClearFocus_Click, {0}", FormatFocus());
}
void Log(string format, params object[] args)
{
textBoxOutput.AppendText(String.Format(format, args) + Environment.NewLine);
textBoxOutput.CaretIndex = textBoxOutput.Text.Length;
textBoxOutput.ScrollToEnd();
}
string FormatType(object obj)
{
return obj != null ? obj.GetType().Name : Null;
}
string FormatFocus()
{
return String.Format("focus: {0}, keyboard focus: {1}",
FormatType(FocusManager.GetFocusedElement(this)),
FormatType(Keyboard.FocusedElement));
}
}
}
[UPDATE]让我们稍微修改一下代码:
void btnClearFocus_Click(object sender, RoutedEventArgs e)
{
//FocusManager.SetFocusedElement(this, this);
FocusManager.SetFocusedElement(this, null);
Keyboard.ClearFocus();
CommandManager.InvalidateRequerySuggested();
Log("btnClearFocus_Click, {0}", FormatFocus());
}
现在我们有了另一个有趣的案例:没有逻辑焦点,没有键盘焦点,但是命令仍然被第二个按钮触发,到达顶部窗口的处理程序并被执行(我认为这是正确的行为):
【问题讨论】:
-
IsFocusScope 应位于包含堆栈面板上,而不是控件本身。
-
感谢您的想法,但是将
IsFocusScope="True"移出到包含StackPanel并不会改变所描述的在没有焦点时的行为。然而,它确实以这种方式改变了路由语义:如果TextBox被聚焦并且对于MainWindow.MyCommand(与顶部窗口相同)具有自己的绑定,则生成的命令按第二个按钮永远不会到达TextBox。顶部的窗户会先吞下它。这是不可取的:我希望首先让聚焦的 UI 元素有机会处理RoutedUICommand,在没有特定目标的情况下触发。