Sudh
Sudh

Reputation: 1335

Event handling issue inside the datagrid..A BUG?

I don't know why I land up with a strange situation in datagrid every another day. This time it is really annoying. So I was trying to handle the Enter key inside the datagrid but there seems to be a problem. As we know default behaviour of Enter key in datagrid is to move down a row, while what I wanted was to do something else, Now I dont know why but even if I override that behaviour using keydown event it refuses to do so. Here is the xaml code:

<DataGrid Grid.Row="1" AutoGenerateColumns="False" x:Name="dataGrid1" VerticalAlignment="Stretch" CanUserReorderColumns="False" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="White" VerticalGridLinesBrush="White" Background="Transparent" Foreground="White" CanUserResizeRows="False" Margin="8,0,8,48" BorderBrush="White" BorderThickness="2" RowBackground="#FF008284" MinRowHeight="5" FontSize="14" ItemsSource="{Binding  }" Grid.RowSpan="2"   SelectionMode="Single" SelectionUnit="FullRow" KeyDown="dataGrid1_KeyDown" SelectedIndex="0">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Background" Value="#FF008284"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Visibility" Value="Visible"/>
            <Setter Property="Height" Value="40"/>

            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="BorderThickness" Value="1,1,1,1"/>
            <Setter Property="BorderBrush" Value="White"/>
        </Style>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="BorderThickness" Value="1,0,1,0"/>
            <Setter Property="BorderBrush" Value="White" />
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="Height" Value="30"/>
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
            <Setter Property="TextBlock.FontSize" Value="14" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Width="80*" Header="Date"   CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= date}" IsReadOnly="True"/>

        <DataGridTextColumn Width="80*" Header="Payment" CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= Payment}" FontSize="16" IsReadOnly="True"/>
        <DataGridTextColumn Width="80*" Header="Receipt" CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= Receipt}" FontSize="16" IsReadOnly="True"/>
        <DataGridTextColumn Width="80*" Header="Balance" CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= Balance}" FontSize="16" IsReadOnly="True"/>
        <DataGridTextColumn Width="80*" Header="Debit" CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= Debit}" FontSize="16" IsReadOnly="True"/>
        <DataGridTextColumn Width="80*" Header="Credit" CanUserResize="False" CanUserReorder="False" CanUserSort="False" Binding="{Binding Path= Credit}" FontSize="16" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

and here is the code behind, i.e which should be invoked:

private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        String date = (dataGrid1.Items[(dataGrid1.SelectedIndex-1)] as DailyTransaction).date

        EnterTransaction transaction = new EnterTransaction(DateTime.Parse(date));

        transaction.ShowDialog();
        ListofTransaction.reloadData();
        return;
    }
    else if (e.Key == Key.F2)
    {
        insertNewRow();
        return;
    }
    else if (e.Key == Key.C)
    {
        ignoreSundays = true;
        insertNewRow();
        ignoreSundays = false;

    }
    else if (e.Key == Key.Escape)
    {
        this.Close();
    }
}

now interesting thing is Escape key works fine in this scenario. I dont know whats the problem here , or may be I am missing something very simple??

Upvotes: 0

Views: 492

Answers (2)

Tigran
Tigran

Reputation: 62246

In addition to all other posts, I would strongly suggest in this case, when you are going to override default behaviuor, create your DataGrid class by inheriting from that one of BCL, and OVERRIDE an event not just subscribe to it.

As first in this case runs builtin code and after raises an event, so your code executed. By overriding event in derived class you can change a behaviuor.

Upvotes: 0

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

Use PreviewKeyDown instead of KeyDown

<DataGrid PreviewKeyDown="dataGrid_PreviewKeyDown"
          ... />

Event Handler

private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        // Do stuff..
        e.Handled = true;
    }
}

Upvotes: 1

Related Questions