WPF - 一个文本框之外时,点击删除焦点 [英] WPF - Remove focus when clicking outside of a textbox

查看:864
本文介绍了WPF - 一个文本框之外时,点击删除焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本框,我想重点表现得有点不同于正常的WPF应用程序。基本上,我想他们表现得更像一个文本框,在网页上的行为。也就是说,如果我的任意位置单击文本框之外,它可能会失去重点。什么是这样做的最佳方式是什么?

I have some textboxes where I would like focus to behave a little differently than normal for a WPF application. Basically, I would like them to behave more like a textbox behaves on a webpage. That is, if I click anywhere outside of the textbox, it will lose its focus. What is the best way to do so?

如果答案是编程方式删除的重点,什么是探测范围外的鼠标点击的最好方法?如果我点击将成为焦点的新的收件人元素?

If the answer is to programmatically remove focus, what is the best way to detect a Mouseclick outside of the bounds? What if the element I'm clicking on will be the new recipient of focus?

推荐答案

而不是增加新的控制窗口,我想你应该添加名称,您的和点击甚至窗口将焦点设置网格是这样的:

rather than adding new control to window i think you should add name to your grid and on click even of window set focus on grid something like this:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="412" Width="569" MouseDown="Window_MouseDown" Name="window1" >
    <Grid ShowGridLines="False" KeyDown="Grid_KeyDown" Name="grid1" Focusable="True">
          <TextBox HorizontalAlignment="Left" Margin="117,61,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>



后面的代码:

code behind:

private void window1_MouseDown(object sender, MouseButtonEventArgs e)
{
    grid1.Focus();
}

这篇关于WPF - 一个文本框之外时,点击删除焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆