Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
770
XamGrid.KeyDown doesn't pass Key.Enter
posted

Hi

I wanted to catch 'enter' in a text column to add a new line to the text (since TextColumn doesn't support 'AcceptsReturn') so I added a KeyDown handler to the grid and found that the handler isn't called for Key.Enter.

It is, however, called for Key.Enter when catching KeyDown on a TextBlock in a TemplateColumn. which is what I'm doing now, but it seems an odd restriction.  

 

James

Parents
No Data
Reply
  • 6912
    posted

    Hi,

    The XamGrid handles KeyDown event when the Enter key is press.

    For the case that you describe you can use a TemplateColumn like that:

    ...
    <ig:TemplateColumn Key="String">
        <ig:TemplateColumn.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding String}" />
            </DataTemplate>
        </ig:TemplateColumn.ItemTemplate>
        <ig:TemplateColumn.EditorTemplate>
            <DataTemplate>
                <TextBox Text="{Binding String, Mode=TwoWay}" KeyDown="TextBox_KeyDown" />
            </DataTemplate>
        </ig:TemplateColumn.EditorTemplate>
    </ig:TemplateColumn>
    ...
    
    ...
    private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        TextBox source = (TextBox)sender;
    
        if (e.Key == Key.Enter)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
            {
                this.XGrid.ExitEditMode(false);
            }
            else
            {
                int start = source.SelectionStart;
                source.Text = source.Text.Insert(source.SelectionStart, Environment.NewLine);
                source.SelectionStart = start + Environment.NewLine.Length;
                e.Handled = true;
            }
        }
    }
    ...
    

    I hope that this will help you

Children