Click here to Skip to main content
15,068,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code.... i am facing problem with text box handler... problem with handler is that when i am try to enter any value in text box it is automatically event is calling for first key press only.... So is there any other event which allows user to enter data into text box an then the event should call like "On Blur"???? If anyone can do please help me bye providing code....Code is done everything dynamically no xaml is used
C#
private void CreateDynamicGridView(DataSet dsBillingDetails)
       {

          // Create a GridView
           int rowPosition = 0;
           for (int index = 1; index < dsBillingDetails.Tables.Count; index++)
           {

                   DataGrid grdCharges = new DataGrid();
                   grdCharges.ItemsSource = dsBillingDetails.Tables[index].DefaultView;
                   grdCharges.AutoGenerateColumns = false;

                   Style grdHeaderStyle = this.FindResource("grdHeaderStyle") as Style;

                   <big>//Adding Button Dynamically</big>
                   DataGridTemplateColumn dgtcDel = new DataGridTemplateColumn();
                   FrameworkElementFactory fefDelButton = new FrameworkElementFactory(typeof(Button));
                   BitmapImage biDeleteIcon = new BitmapImage();

                   fefDelButton.SetBinding(Button.TagProperty, new Binding("LineItemID"));
                   fefDelButton.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnDelete_Click), true);
                   DataTemplate buttonTemplate = new DataTemplate();

                   dgtcDel.Header = "D";
                   dgtcDel.HeaderStyle = grdHeaderStyle;
                   buttonTemplate.VisualTree = fefDelButton;
                   dgtcDel.CellTemplate = buttonTemplate;
                   biDeleteIcon.BeginInit();
                   biDeleteIcon.UriSource = new Uri(@"..\Images\Deleteicon.png", UriKind.Relative);
                   biDeleteIcon.EndInit();

                   fefDelButton.SetValue(Image.SourceProperty, biDeleteIcon);
                   fefDelButton.SetValue(Image.VisibilityProperty, Visibility.Visible);
                   grdCharges.Columns.Add(dgtcDel);

                   //create a template column for text box
                   DataGridTemplateColumn dgtTextBox = new DataGridTemplateColumn();

                   //set title of column
                   dgtTextBox.Header = "Line Item Units";
                   dgtTextBox.Width = 150;

                   DataTemplate dtTextBox = new DataTemplate();
                   FrameworkElementFactory fefTextBox = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));

              //<big>Adding Hanler THIS IS THE ISSUE</big>
                 fefTextBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(txtUnitCount_Changed));
                   dtTextBox.VisualTree = fefTextBox;
                   dgtTextBox.CellTemplate = dtTextBox;
                   grdCharges.Columns.Add(dgtTextBox);

                   grdBillingDetails.Children.Add(grdCharges);

                   //   grdCharges.Style = grdHeaderStyle;
                   Grid.SetRow(grdCharges, rowPosition + 1);
                   rowPosition = rowPosition + 2;
               }
       }
Posted
Updated 23-Oct-13 4:27am
v3

1 solution

It is doing that because when you enter text into the textbox, the TextChanged event is fired.

I think "OnBlur" would be a viable alternative since it fires when the control loses focus.

[EDIT]

Have a look at these articles:

Click[^]

and

LostFocus[^]

It appears as though "LostFocus" is the event for you.
   
v2
Comments
jing567 23-Oct-13 10:31am
   
Hello richcb,
I am familaiar with on blur event in asp.net, But right now i am using wpf windows application,now i has to attach event for FrameworkElementFactory.. here there is no such event like on blur... So if u have done anything in past... please provide me the code...
Richard C Bishop 23-Oct-13 10:38am
   
See my update to the solution. It speaks of the "LostFocus" event that is roughly the equivalent to "onblur" in web development.
jing567 23-Oct-13 10:42am
   
Can u provide me the c# code for UpdateSourceTrigger=LostFocus so that i can add to my code and check whether it is working or not.... and one more thing is that how to bind event for the text box if we place UpdateSourceTrigger... I mean for which event???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900