Invalid key event handling on text box in datagrid
Last post 05-15-2008 8:00 AM by joer00. 6 replies.
Sort Posts:
05-08-2008 8:58 AM
Invalid key event handling on text box in datagrid

I try to work around the bug i reported that textbox with accept return does not work in data grid. Looking into the data grid code I see that key events are not processes if the handled is set to true. So I thought I wrap my textbox in a grid and call set handled=true in the grids keydown event.

 This indeed prevents the data grid from processing, but now I can only enter backspace in my text cell ! As far as I understand events bubble UP not down as it is possible in WPF. From debug I can see that the TextBox key down event is indeed fired BEFORE the Grids key down event. So why can't I enter text ?

<data:DataGridTemplateColumn.CellEditingTemplate>

<DataTemplate>

<Grid KeyDown="Grid_KeyDown">

<TextBox AcceptsReturn="True" x:Name="tbColumn0" Text="{Binding Description, Mode=TwoWay}"

Style="{StaticResource TextBoxDescription}" />

</Grid>

</DataTemplate>

</data:DataGridTemplateColumn.CellEditingTemplate>

 

private void Grid_KeyDown(object sender, KeyEventArgs e)

{

e.Handled =
true;

}

Joe Robe

joer00

Joined on 07-05-2006
Posts 72
05-13-2008 3:03 AM
Re: Invalid key event handling on text box in datagrid

Hello, you can manually insert a line break to a TextBox in a DataGrid. Try something like this:

private void tbColumn0_KeyDown(object sender, KeyEventArgs e)

{

if (e.Key == Key.Enter)

{

TextBox tb = (TextBox)sender;

StringBuilder sb = new StringBuilder();

//save the current SelectionStart.

int start = tb.SelectionStart;

//Insert '\n' to the text.

sb.Append(tb.Text.Substring(0, start));

sb.Append(
'\n');

sb.Append(tb.Text.Substring(start + tb.SelectionLength));

tb.Text = sb.ToString();

//Set the caret to just after the line break.

tb.Select(start + 1, 0);

e.Handled =
true;

}

}

 

 

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Joined on 10-29-2007
Posts 1,084
05-13-2008 9:17 AM
Re: Invalid key event handling on text box in datagrid

As I mentioned in my previous post, setting the e.handled to true results in NO key accepted in the textbox but the backspace key. It might work to manually construct the text box text for ANY key input. I did not check this as I worked around the problem by putting a non gird text box above the gird one on enter. 

Joe Robe

joer00

Joined on 07-05-2006
Posts 72
05-13-2008 11:03 PM
Re: Re: Invalid key event handling on text box in datagrid

You don't need to handle all keys. You only need to handle the Enter key.

if (e.Key == Key.Enter)

{

//Handle Enter yourself.

e.Handled =
true;

}

//Other keys will still be handled by the TextBox.

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Joined on 10-29-2007
Posts 1,084
05-14-2008 7:54 AM
Re: Re: Invalid key event handling on text box in datagrid

 Yi Lun,

 as I posted in this thread http://silverlight.net/forums/p/15454/51043.aspx#51043 calling e.Cancel results in NO KEY entered into the text box ! That's why I was posting one has to try to manually add ALL keys. So please try it our, maybe my setup is special as I use a custom control in the details view of the DataGrid row. And once again, setting e.Cancel on the KeyDown event of the gird results in NO KEY appearing in the text box.
 

Joe Robe

joer00

Joined on 07-05-2006
Posts 72
05-15-2008 12:19 AM
Marked as Answer
Re: Re: Re: Invalid key event handling on text box in datagrid

Yes, I understand setting e.Handled to true will prevent TextBox from entering keys. That's why I suggest you only to set e.Handled to true when the key is Enter. For other keys, you can keep e.Handled as false. Maybe this will make you feel more confertable:

if (e.Key == Key.Enter)

{

//Handle Enter yourself.

e.Handled =
true;

}

else

{

e.Handled = false;

}

 

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Joined on 10-29-2007
Posts 1,084
05-15-2008 8:00 AM
Re: Re: Re: Invalid key event handling on text box in datagrid

 ah sorry, stupid me I did not read it correctly :) That should work now.

 Thanks
 

Joe Robe

joer00

Joined on 07-05-2006
Posts 72