本文介绍了WAPF更改事件中的TextBox更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我在wpf c#window应用程序中有一个文本框。我在c#中动态地在数据网格中创建它。我已经在该文本框中附加了一个事件处理程序。但问题是当控件加载到其中时,文本框事件被无限调用...完成代码正在c#中完成只...我没有使用xaml ...每个文本框都是动态创建的....注意:文本框出现在数据网格中...请帮助我yar ... private void txtUnitCount_Changed( object Sender,TextChangedEventArgs e) { try { int index = 0 ; int rowIndex = -1; TextBox txt = Sender as TextBox; int Units = Convert.ToInt32(txt.Text); for (index = 1 ; index < dsBillingDetails.Tables.Count; index ++) { DataRowCollection rowCol = dsBillingDetails.Tables [index] .Rows; DataRow [] DataRows = dsBillingDetails.Tables [index] .Select(find); if (DataRows.Count()> 0 ) { DataRow foundRow = DataRows [ 0 ]; rowIndex = rowCol.IndexOf(foundRow); if (rowIndex!= -1) { / / 此行导致问题,即行下面 dsBillingDetails.Tables [index] .AcceptChanges(); } break ; } } } catch (例外情况) {} } 解决方案 以下是如何: myElement.GotFocus + = {sender,eventArgs} = > { UIElement senderElement =(UIElement)sender; DoSomethingOnChangeOfTheFocusState(senderElement.IsFocused); // 如果获得焦点,IsFocused将为true,如果丢失则为false }; 请注意,事件名称GotFocus有点误导。它实际上涵盖了获得和失去焦点。与事件相同 UIElement.GotKeyboardFocus 和 UIElement.PreviewGotKeyboardFocus : http://msdn.microsoft.com/en-us /library/system.windows.uielement.gotfocus%28v=vs.110%29.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.windows .uielement.gotkeyboardfocus%28v = vs.110%29.aspx [ ^ ], HTTP://msdn.mi crosoft.com/en-us/library/system.windows.uielement.previewgotkeyboardfocus%28v=vs.110%29.aspx [ ^ ]。 可选方法是覆盖相应的虚拟方法,效果相同: http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotfocus(v = vs.110)的.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotkeyboardfocus(v = vs.110).aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system .windows.uielement.onpreviewgotkeyboardfocus(v = vs.110).aspx [ ^ ]。 -SA Hello, I am having a text box in wpf c# window application. I am creating it in c# dynamically in Data grid.I have attached a event handler to that text box.But problem is that when control is loaded into it the text box event is being called infinitely ...Complete code is being done in c# only... I am not using xaml ... every text box is being created dynamically.... Note: the text box is present in Data Grid... Please help me out yar...private void txtUnitCount_Changed(object Sender, TextChangedEventArgs e) { try { int index = 0; int rowIndex = -1; TextBox txt = Sender as TextBox; int Units = Convert.ToInt32(txt.Text); for (index = 1; index < dsBillingDetails.Tables.Count; index++) { DataRowCollection rowCol = dsBillingDetails.Tables[index].Rows; DataRow[] DataRows = dsBillingDetails.Tables[index].Select(find); if (DataRows.Count() > 0) { DataRow foundRow = DataRows[0]; rowIndex = rowCol.IndexOf(foundRow); if (rowIndex != -1) { //This line is causing problem i.e below line dsBillingDetails.Tables[index].AcceptChanges(); } break; } } } catch (Exception ex) { } } 解决方案 Here is how:myElement.GotFocus += {sender, eventArgs} => { UIElement senderElement = (UIElement)sender; DoSomethingOnChangeOfTheFocusState(senderElement.IsFocused); // IsFocused will be true if got focus, false if lost};Note that the event name "GotFocus" is somewhat misleading. It actually covers both getting and losing focus. Same thing with the events UIElement.GotKeyboardFocus and UIElement.PreviewGotKeyboardFocus:http://msdn.microsoft.com/en-us/library/system.windows.uielement.gotfocus%28v=vs.110%29.aspx[^],http://msdn.microsoft.com/en-us/library/system.windows.uielement.gotkeyboardfocus%28v=vs.110%29.aspx[^],http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewgotkeyboardfocus%28v=vs.110%29.aspx[^].The optional approach is to override corresponding virtual methods, with the same effect:http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotfocus(v=vs.110).aspx[^],http://msdn.microsoft.com/en-us/library/system.windows.uielement.ongotkeyboardfocus(v=vs.110).aspx[^],http://msdn.microsoft.com/en-us/library/system.windows.uielement.onpreviewgotkeyboardfocus(v=vs.110).aspx[^].—SA 这篇关于WAPF更改事件中的TextBox更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 08:14