Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi,
 
I am about 1month new to C# and WPF. Would appreciate some help with the following:
 
I have a DataGridTextColumn and DataGridTemplateColumn.
I have prepopulated the table row/column with some sample data.
When a user enters some text in the KEY column row, I would like to:
 
1. Determine the text that the user has entered - event drivien.
2. Based on (1), populate the row with additional data pertaining to the specific KEY row/column data.
 
I have the following in my DataGridTextColumn:
 
<DataGridTextColumn.EditingElementStyle>
  <Style TargetType="{x:Type TextBox}">
    <eventsetter event="TextInput" handler="TEXT_ENTERED" />
  </Style>
 
 
In my C# code,
 
private void TEXT_ENTERED(object sender, RoutedEventArgs e)
{
  TextBox t = (TextBox)sender;
  string name = t.Text;
  Debug.WriteLine(name);
}
 
The TEXT_ENTERED function does not work when the Event="TextInput". When I set the Event to "TextChanged", then I get an event for each character entered in the DataGridTextColumn.
 
I want to capture the whole text entered - either by entered in "ABC" followed by a carriage return, a mouse click elsewhere....etc.
 
Any help would be appreciated.
 
Another funny thing is that the above works for "TextInput" in Visual Studio 2010, but not in 2008.
 
Thanks,
 
Manish
Posted 8-Jul-11 10:26am
Edited 8-Jul-11 10:28am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Actually found a solution to this, although it is not the best one:
 
Instead of using TextInput or TextChanged, I used KeyDown as the event.
In my TEXT_ENTERED method, I used KeyEventArgs instead of RoutedEventArgs.
Finally, I did:
 
if(e.Key == Key.Return)
{
  // Get the Text
}
 
I wish things were a bit more staightforward....;)
 
Full snippet:
 
WPF
<datagridtextcolumn.editingelementstyle>>
  <Style TargetType="{x:Type TextBox}">
    <eventsetter event="KeyDown" handler="TEXT_ENTERED" />
  </Style>
 
 

 
C#
private void TEXT_ENTERED(object sender, RoutedEventArgs e)
{
  if(e.Key == Key.Return)
  {
    TextBox t = (TextBox)sender;
    string name = t.Text;
    Debug.WriteLine(name);
  }
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 8 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100