DataSrid行突出显示正在ItemSource上删除刷新

时间:2016-02-26 10:01:03

标签: c# wpf xaml datagrid

我有DataGrid People我可以删除People。删除Person后,我调用以下方法;

private void SetSelectedPerson(int personDeleted, int personID)
{
    if (peopleDataGrid.SelectedIndex > 0)
    {
        peopleDataGrid.SelectedIndex--;
    }
    else
    {
        peopleDataGrid.SelectedIndex = 0;
    }  
}

这会选择之前的Row,但会从SelectedRow中删除突出显示。这不是我想要的,尽管SelectedIndex是正确的,但看起来并不像用户选择了Row

我确实刷新了DataGrid ItemSource,以便从当前Person中删除已删除的Collection。如果我不刷新ItemSource,则突出显示是正确的,但当然不会从Person中删除已删除的DataGrid

我使用;

刷新ItemSource
  People = await ReturnPeople();
  PeopleICollectionView = CollectionViewSource.GetDefaultView(People);
  peopleDataGrid.ItemsSource = PeopleICollectionView;
  DataContext = this;

如何在Row更新中突出显示ItemSource

编辑:对于Sakura

如果我删除了DataGrid的硬代码,这就是我绑定ItemSource的方式..

 People = await ReturnPeople();
 PeopleICollectionView = CollectionViewSource.GetDefaultView(People);
 DataContext = this;

XAML;

        <DataGrid x:Name="peopleDataGrid" IsReadOnly="true" Margin="10,15,10,5" ColumnWidth="*" FontSize="14" 
                      HeadersVisibility="Column" AutoGenerateColumns="False" CanUserAddRows="False" 
                      SelectionChanged="DataGridSelectionChanged" ItemsSource="{Binding PeopleICollectionView}">

但是当我删除一个人时,它不会在DataGrid ...

中更新

1 个答案:

答案 0 :(得分:1)

  

删除一个人后,我调用以下方法......

首先,如果您的DataGrid是单一选择模式,当您删除所选项目时,所选索引将设置为-1。如果它是多选模式,它将返回另一个所选项的索引,如果没有,则返回-1。因此,您必须在删除行之前获取所选的datagrid 索引。然后在删除后使用此值。

其次,如果您的数据网格绑定正确,则无需刷新datacontext即可进行更新。
如果您有兴趣解决此问题,请在此处发布您的绑定代码。

相关问题