WindowsFormsHost甚至通过其他WPF形式也失去了对激活应用程序的关注 [英] WindowsFormsHost steals focus on activating application even by other wpf form
问题描述
-
运行Project并设置专注于MainWindow.xaml(在WindowsFormsHost中)的文本框
有什么主意吗?
< Window x:Class =" WinFormsHostFocusProblem.MainWindow" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:WinFormsHostFocusProblem" xmlns:my ="clr-namespace:System.Windows.Forms.Integration; assembly = WindowsFormsIntegration" 标题="MainWindow".高度="350".宽度="525" < Grid> < my:WindowsFormsHost Focusable ="False" > < local:UserControl1> </local:UserControl1> </my:WindowsFormsHost> </Grid></Window>
命名空间WinFormsHostFocusProblem { 公共局部类MainWindow:Window { 公共MainWindow() { InitializeComponent(); Window1 window1 = new Window1(); window1.Show(); } }}
< Window x:Class =" WinFormsHostFocusProblem.Window1" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:WinFormsHostFocusProblem" xmlns:my ="clr-namespace:System.Windows.Forms.Integration; assembly = WindowsFormsIntegration" SizeToContent =" WidthAndHeight" ResizeMode ="NoResize". Topmost ="True" 标题="Window1".高度="300".宽度="300".背景=红色" < Grid> < TextBox Height =" asd</TextBox> </Grid></Window>
解决方案
您好yunusayd,
按照您的逐步说明,我已经测试了您的代码,但是我无法解决您的问题.一切正常.
对于这些类似的情况,我曾经吃过饭,请查看下面的反馈和主题,并注意,在您的情况下,Window1中的TextBox将成为焦点.
http://social.msdn.microsoft .com/Forums/zh-CN/wpf/thread/30e21265-2c14-4b3d-bde4-df803dee3c3c
希望有帮助.
如果问题仍然存在,请提供有关如何进行修复的更多详细信息.
谢谢.
今天愉快.
To Reproduce my case (.net 4.0)
- Create a WPF Application (MainWindow.xaml)
- Add a Winform user control that contains a textbox (UserConrol1.cs - Winform)
- Put UserControl1 into MainWindow.xaml with windowsformshost
- Add another WPF Window that contains a textbox(wpf) to project (Window1.xaml)
- Create and Show Window1 after MainWindow InitializeComponent
Your project is ready,
- Run Project and set textbox focused in MainWindow.xaml (that in WindowsFormsHost)
- Deactivate your application by opening a window (Windows file explorer ,notepad, winamp etc.)
- Try to write in textbox that in Window1 window by clicking textbox with mouse
And you will see that you can't set focus on textbox in Window1 because MainWindow Texbox( in winformshost will steal your focus on you application got activating)
Any idea?
MainWindow.xaml
<Window x:Class="WinFormsHostFocusProblem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WinFormsHostFocusProblem" xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" Title="MainWindow" Height="350" Width="525"> <Grid> <my:WindowsFormsHost Focusable="False" > <local:UserControl1> </local:UserControl1> </my:WindowsFormsHost> </Grid></Window>MainWindow.xaml.cs
namespace WinFormsHostFocusProblem{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Window1 window1 = new Window1(); window1.Show(); } }}
Window1.xaml
<Window x:Class="WinFormsHostFocusProblem.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WinFormsHostFocusProblem" xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Topmost="True" Title="Window1" Height="300" Width="300" Background="Red"> <Grid> <TextBox Height="25">asd</TextBox> </Grid></Window>
解决方案Hi yunusayd,
I test your code, as you said step by step, but I can't repro your issue. Every thing works fine.
And for these similar kinds of situations, I have ever meat, please check out below feedback and thread, and payattention that in your situation, the TextBox in Window1 will get focus corecctly.
https://connect.microsoft.com/VisualStudio/feedback/details/707184/textbox-in-pop-up-window-cant-get-keyboard-focus-after-some-controls-in-windowsformhost-lost-focus
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/30e21265-2c14-4b3d-bde4-df803dee3c3c
Hope it helps.
If issue still exist, please give a more detail information about how to repro it.
Thank you.
Have a nice day.
这篇关于WindowsFormsHost甚至通过其他WPF形式也失去了对激活应用程序的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文