数据网格中的Lostfocus事件

时间:2012-10-15 08:55:58

标签: wpf

我希望为wpf中的数据网格中的列实现丢失的焦点事件。我已经使用过CellEditEnding事件,但是在编辑开始时却没有提到这个事件。请有人帮助我。

3 个答案:

答案 0 :(得分:0)

我已经使用过那个事件而且它的工作非常完美。 我的xaml代码看起来像

<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490"  ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding"  CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
                        <DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Update" x:Name="btnUpdate"
                            Click="btnUpdate_Click"></Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Delete" x:Name="btnDelete"
                            Click="btnDelete_Click"></Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

在我的.cs文件中代码看起来像

   private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
        var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
        foreach (var item in update)
            {
            item.ID = objEmpToEdit.ID;
            item.Description = objEmpToEdit.Description;
            item.Date = objEmpToEdit.Date;
            item.Amount = objEmpToEdit.Amount;
            item.Remark = objEmpToEdit.Remark;
            }
        Context.SubmitChanges();
        MessageBox.Show("The Current row updation is complete..");
        }


  private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

        if (isUpdateMode) //The Row is edited
            {
            Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
            FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
            if (element_1.GetType() == typeof(TextBox))
                {
                var Description = ((TextBox)element_1).Text;
                objEmpToEdit.Description = Description.ToString();

                }

            FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
            if (element_2.GetType() == typeof(TextBox))
                {
                var Amount = ((TextBox)element_2).Text;
                objEmpToEdit.Amount = Amount.ToString();
                }

            FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
            if (element_3.GetType() == typeof(TextBox))
                {
                var Date = ((TextBox)element_3).Text;
                objEmpToEdit.Date = Convert.ToDateTime(Date);
                }

            FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
            if (element_4.GetType() == typeof(TextBox))
                {
                var Remark = ((TextBox)element_4).Text;
                objEmpToEdit.Remark = Remark.ToString();
                }


            }


        }
 private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
        }

答案 1 :(得分:0)

为什么不在单元格上使用LostFocus事件,然后检查单元格是否属于您想要的列。

例如:

<DataGrid.CellStyle>
   <Style TargetType="{x:Type DataGridCell}">
      <EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
   </Style>
</DataGrid.CellStyle>

然后在处理程序中:

private void OnCellLostFocus(object sender, RoutedEventArgs e)
    {
        if (((DataGridCell)sender).Column.Header == "My Column")
            //do stuff
    }

答案 2 :(得分:0)

Esam works提供的解决方案需要一些编辑:

XAML保持不变:

  <DataGrid.CellStyle>
   <Style TargetType="{x:Type DataGridCell}">
      <EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
   </Style>
</DataGrid.CellStyle>

后面要修改的代码:

private void OnCellLostFocus(object sender, RoutedEventArgs e)
    {
        var myCell = sender as DataGridCell;
        if (myCell.Column.Header.ToString() == "Name of the column")
        {
            MessageBox.Show("tralala. I got it");
            //do whatever needed here
        }
    }
相关问题