datagrid里面的事件处理问题......一个BUG?

时间:2011-08-15 16:38:19

标签: c# .net wpf datagrid event-handling

我不知道为什么每隔一天我就会在数据网格中遇到一个奇怪的情况。这次真的很烦人。所以我试图在datagrid中处理Enter键,但似乎有问题。我们知道数据网格中的Enter键的默认行为是向下移动一行,而我想要的是做其他事情,现在我不知道为什么,但即使我使用keydown事件覆盖该行为,它也拒绝这样做。 这是xaml代码:

<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>

这里是后面的代码,即应该调用的代码:

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();
    }
}

现在有趣的是Escape键在这种情况下工作正常。我不知道这里的问题是什么,或者可能是我错过了一些非常简单的事情?

2 个答案:

答案 0 :(得分:1)

使用PreviewKeyDown代替KeyDown

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

事件处理程序

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

答案 1 :(得分:0)

除了所有其他帖子之外,我强烈建议在这种情况下,当你要覆盖默认行为时,通过继承BCL中的那个来创建你的DataGrid类,并且OVERRIDE事件不仅仅是订阅它。 / p>

在这种情况下,首先运行内置代码并在引发事件之后,执行代码。通过覆盖派生类中的事件,您可以更改行为。