相关疑难解决方法(0)

WebBrowser控制键盘和焦点行为

显然,WPF WebBrowser控件存在一些严重的键盘和焦点问题.我整理了一个简单的WPF应用程序,只是一个WebBrowser和两个按钮.该应用程序加载一个非常基本的可编辑HTML标记(<body contentEditable='true'>some text</body>)并演示以下内容:

  • 标签是行为不端的.用户需要按两次Tab键才能在WebBrowser中查看插入符号(文本光标)并能够键入.

  • 当用户切换远离应用程序时(例如,使用Alt-Tab),然后返回,插入符号消失,她根本无法键入.需要物理鼠标单击WebBrowser的窗口客户端区域才能取回插入符号和击键.

  • 不一致的是,一个虚线的焦点矩形出现在WebBrowser周围(当标签时,但不是点击时).我找不到摆脱它的方法(FocusVisualStyle="{x:Null}"没有帮助).

  • 在内部,WebBrowser永远不会得到关注.对于逻辑焦点(FocusManager)和输入焦点(键盘)都是如此.将Keyboard.GotKeyboardFocusEventFocusManager.GotFocusEvent从不事件被解雇的web浏览器(虽然他们都对按钮做在同一个焦点范围).即使当插入符号是web浏览器内,FocusManager.GetFocusedElement(mainWindow)指向先前聚焦元件(按钮)和Keyboard.FocusedElementnull.同时,((IKeyboardInputSink)this.webBrowser).HasFocusWithin()回报true.

我会说,这样的行为几乎太功能无法实现,但这就是它的运作方式.我可能想出一些黑客来解决它并将其与原生WPF控件一起排成一行TextBox.我仍然希望,也许我在这里错过了一些模糊而简单的东西.有没有人处理过类似的问题?任何有关如何解决这个问题的建议将不胜感激.

此时,我倾向于为基于HwndHost的 WebBrowser ActiveX控件开发内部WPF包装器.我们还在考虑 WebBrowser的其他替代方案,例如Chromium Embedded Framework(CEF).

VS2012项目可以从这里下载,以防有人想玩它.

这是XAML:

<Window x:Class="WpfWebBrowserTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Width="640" Height="480" Background="LightGray">

    <StackPanel Margin="20,20,20,20">
        <ToggleButton Name="btnLoad" Focusable="True" IsTabStop="True" Content="Load" Click="btnLoad_Click" Width="100"/>

        <WebBrowser Name="webBrowser" Focusable="True" KeyboardNavigation.IsTabStop="True" FocusVisualStyle="{x:Null}" Height="300"/>

        <Button Name="btnClose" Focusable="True" IsTabStop="True" Content="Close" Click="btnClose_Click" …
Run Code Online (Sandbox Code Playgroud)

keyboard wpf focus webbrowser-control wpf-controls

28
推荐指数
2
解决办法
9886
查看次数

当焦点在WindowsFormsHost中时,为什么Keyboard.FocusedElement为null?它打破了WPF命令路由

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

<Window.CommandBindings>
    <CommandBinding Command="local:MainWindow.MyCommand" CanExecute="CanExecuteCommmand" Executed="CommandExecuted"/>
</Window.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

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

我已经发现这种情况正在发生,因为Keyboard.FocusedElementnull焦点在里面的时候WindowsFormsHost.为什么null在这种情况下,它是WPF错误还是设计功能?我错过了什么吗?

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

项目来源可在此处获得.

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" /> …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf xaml winforms

6
推荐指数
1
解决办法
1863
查看次数

我必须使用CommandTarget吗?我认为任何有针对性的元素都会收到命令

我正在尝试了解如何使用RoutedCommands.我的印象是,如果我没有在Button上指定CommandTarget,任何聚焦元素都将收到命令.但由于某种原因,它不起作用.这是不起作用的xaml:

<Window x:Class="WpfTest11_Commands2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="177" HorizontalAlignment="Left"
            Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
        <TextBox Height="177" HorizontalAlignment="Left"
            Margin="258,12,0,0" Name="textBox2" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
        <Button Content="Cut"
                    Height="23" HorizontalAlignment="Left" Margin="12,195,0,0" Name="button1" VerticalAlignment="Top" Width="75"
                    Command="ApplicationCommands.Cut"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

如果我将CommandTarget添加到Button它可以工作,但仅适用于当然指定的文本框.

<Window x:Class="WpfTest11_Commands2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="177" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
        <TextBox Height="177" HorizontalAlignment="Left" Margin="258,12,0,0" Name="textBox2" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
        <Button Content="Cut"
                    Height="23" HorizontalAlignment="Left" Margin="12,195,0,0" Name="button1" VerticalAlignment="Top" Width="75"
                    Command="ApplicationCommands.Cut"
                    CommandTarget="{Binding ElementName=textBox1}"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

如何使任何聚焦元素接收命令?

谢谢!

wpf xaml routed-commands

4
推荐指数
1
解决办法
2671
查看次数