c# - 当焦点位于 WindowsFormsHost 内时,为什么 Keyboard.FocusedElement 为空?它打破了 WPF 命令路由

标签 c# .net wpf winforms xaml

我有一个自定义 RoutedUICommand MyCommand 通过 ICommand.Execute 执行.顶部窗口有一个绑定(bind)来处理它:

<Window.CommandBindings>
    <CommandBinding Command="local:MainWindow.MyCommand" CanExecute="CanExecuteCommmand" Executed="CommandExecuted"/>
</Window.CommandBindings>

这是此命令的唯一处理程序。我还有一个 WindowsFormsHost内部带有 WinForms 的 TextBox 控件(用于演示目的)。 当焦点在此 TextBox 内时,MyCommand 不会到达顶部窗口。当焦点位于 WPF 的 native TextBox 内时,命令处理程序按预期被调用。

我发现这是因为Keyboard.FocusedElement当焦点位于 WindowsFormsHost 内时为 null。为什么在这种情况下它是 null,是 WPF 错误还是设计功能?我错过了什么吗?

我相信命令应该到达顶部窗口,无论焦点在哪里(当它是可视化树中唯一的处理程序并且 FocusManager.IsFocusScope 设置正确时)。我有一个 related question about that .

项目源可用here .

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" 
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
        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="150" Text="WPF TextBox&#x0a;"/>
        <WindowsFormsHost Focusable="True" KeyboardNavigation.IsTabStop="True"  Height="150">
            <wf:TextBox x:Name="textBoxWf" Text="WinForms TextBox" />
        </WindowsFormsHost>
        <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"/>
    </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));
        }
    }
}

最佳答案

要完成互操作 Form-WPF,您需要执行以下操作:

App.xaml.cs:

    public partial class App : Application
      {
      protected override void OnStartup(StartupEventArgs e)
        {
        WindowsFormsHost.EnableWindowsFormsInterop();
        base.OnStartup(e);
        }
      }

关于c# - 当焦点位于 WindowsFormsHost 内时,为什么 Keyboard.FocusedElement 为空?它打破了 WPF 命令路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437898/

相关文章:

java - .NET 与 Java 在初创公司中进行 Web 应用程序开发

wpf - 父区域导航时如何触发嵌套子区域中的 NavigatingFrom

wpf - 条件数据模板

c# - 如何在android中创建一个选项菜单?

.net - 在包含计数的子查询上使用左连接的 Linq

c# - 通过名称中止线程

c# - 如何更改 TabbedPage 中的默认选定项?

c# - 使用 visual studio 2010 在 Windows 窗体中创建线条?

c# - 使用 ServiceStack 的 Soap12ServiceClient 将请求对象发送到使用 ServiceStack.Factory 配置的服务时出现 404 错误

c# - Monitor.Pulse 和 Monitor.PulseAll 之间的区别