当前位置:编程学习 > C#/ASP.NET >>

WPF中怎么让窗口而不是控件获得焦点

新建一个WPF,拖拽一个TextBox控件

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" KeyDown="Window_KeyDown"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="50,91,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>


程序刚启动的时候,文本框并没有获得焦点,而是主窗口具有焦点,按下键盘后,Window_KeyDown会被触发,同时Keyboard.FocusedElement指示的也是主窗口

在文本框上单击后,文本框就获得了焦点,在这之后,怎么再次让主窗口获得焦点呢?也就是回到刚启动的状态

用this.Focus()和Keyboard.Focus(this)都不行

用Keyboard.ClearFocus()可以清除文本框上的焦点,但是主窗口还是没有获得焦点,因为Window_KeyDown事件不会被触发,
FocusedElement为null

用Keyboard.ClearFocus()+this.Focus()也不行

怎么才可以呢? --------------------编程问答-------------------- 木有一个人回答?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,