Click here to Skip to main content
11,706,799 members (47,240 online)
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 9:26am
Edited 8-Jul-11 9: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 Sergey Alexandrovich Kryukov 140
1 OriginalGriff 125
2 chainerlt 110
3 ppolymorphe 109
4 Mika Wendelius 70
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,487
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 8 Jul 2011
Copyright © CodeProject, 1999-2015
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