检索Datagrid行的值

时间:2014-05-22 19:20:19

标签: c# wpf datagrid

我有一个绑定到数据源并使用下拉列的Datagrid。

<DataGrid Name="SFGrid1" ItemsSource="{Binding Records, 
          UpdateSourceTrigger=PropertyChanged}" Margin="39,33,0,26" 
          SelectionChanged="SFGrid1_SelectionChanged" 
          HorizontalGridLinesBrush="Black" HorizontalAlignment="Left" 
          Width="1067" RowEditEnding="SFGrid1_RowEditEnding" 
          AutoGenerateColumns="False" FontFamily="Cambria">
     <DataGrid.Columns>
          <DataGridTemplateColumn Header="ID" CanUserSort="True"  >
               <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                         <TextBlock Text="{Binding ID}"/>
                    </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>  
          </DataGridTemplateColumn>
     </DataGrid.Columns> 
</DataGrid>

我的RowEditEnding看起来像这样

public void SFGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
     var person = e.Row.Item as Person;

     MessageBox.Show("updated Record:" + person.id);
}

我的问题是,当我从一个单元格移动到另一个单元格时,RowEditEnding并不总是触发。似乎我必须双击另一个单元格,它通常需要几次才能显示新数据。我想要手动触发它,比如从按钮开始,或者至少在我点击新行时触发它......

1 个答案:

答案 0 :(得分:0)

DataGrid.SelectedCellsChanged将是RowEditEnding的绝佳替代品。

<DataGrid Name="SFGrid1" ItemsSource="{Binding Records, 
      UpdateSourceTrigger=PropertyChanged}" Margin="39,33,0,26" 
      SelectionChanged="SFGrid1_SelectionChanged" 
      HorizontalGridLinesBrush="Black" HorizontalAlignment="Left" 
      Width="1067" SelectedCellsChanged ="SFGrid1_SelectedCellsChanged" 
      AutoGenerateColumns="False" FontFamily="Cambria">

通过按下按钮来启动事件似乎是不可能的(对我而言),因为你使用提供的DataGridRowEditEndingEventArgs,如果你手动调用这个函数,它将为null。

相关问题