即使字段为空,如何从文本框中启动事件c#wpf [英] how to launch event from a textbox even when the field is empty c# wpf

查看:89
本文介绍了即使字段为空,如何从文本框中启动事件c#wpf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取数据,然后将其显示在datagrid(wpf)中,用户可以在单击Enter后从文本框中进行快速搜索(过滤). 我使用此事件来处理按钮-Enter-单击 (OnSearch-只要单击搜索按钮或按Enter键,就会产生此事件.)

I get data from the data base then i show it in a datagrid (wpf) , the user can make quick search (filter) from a textbox after clicking on Enter. I use this event to handle the button -Enter- click (OnSearch - This event is spawn whenever the search button is clicked or the Enter key is pressed.)

问题是当用户什么都不写时,即使他单击Enter-Button,事件也不会启动! 我如何继续使其工作

the problem is when the user don't write anything, the event will not be launched even when he click on Enter-Button ! how can i proceed to make it work

   public MainWindow()
   {
       InitializeComponent();
       //m_txtTest is a SearchTextBox
       m_txtTest.OnSearch += new RoutedEventHandler(m_txtTest_OnSearch);    
   }

   void m_txtTest_OnSearch(object sender, RoutedEventArgs e)
   {
      //to get the entered string
      SearchEventArgs searchArgs = e as SearchEventArgs;
      ....
      ....
      ....
      ....
   }

推荐答案

因此,当用户输入内容并按Enter时,它就是处理事件的文本框.

So when your user types something in and presses enter it is the textbox handling the Event.

当文本框没有焦点并且无法处理事件时,他们什么也没输入.

When they don't type anything though the textbox doesn't have focus and cant handle the event.

我要做的是创建一个事件以在窗口或网格上进行搜索.

What I would do is create an event for searching on the window or grid.

像这样的东西

this.OnPreviewKeyDown += new OnPreviewKeyDownEvent;

http://msdn.microsoft.com/en-us/library /ms748948.aspx

你可以这样想

窗口具有焦点

->网格具有焦点

->Grid has focus

->文本框具有焦点

-->textbox has focus

具有焦点的最后一件事(或最内在的元素,就像蛋糕一样)将是首先看到事件的事物.如果您为该UI元素注册了该事件,它将对其进行处理.

What ever the last thing that has focus (or the most inner element, think of it like a cake) is will be the thing that sees the event first. If you have that event registered for that UI element it will handle it.

文本框没有集中显示,因此不会看到该事件

The textbox isn't focused so it wont see the event

这篇关于即使字段为空,如何从文本框中启动事件c#wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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