WAPF更改事件中的TextBox更改事件 [英] TextBox change Event in wpf Change Event
问题描述
您好,
我在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 (例外情况)
{
}
}
请查看我的问题并做出相应澄清。现在,我将回答你的一个后续问题:
jing111写道:...让我知道如何将处理程序添加到文本框中以便丢失焦点事件...
以下是如何:
myElement.GotFocus + = {sender,eventArgs} = < span class =code-keyword>> {
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)
{
}
}
Please see my questions and clarify accordingly. For now, I'll answer to one of your follow-up questions:
jing111 wrote:…let me know how to add handler to a text box for lost focus event…
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
andUIElement.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更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!