I have a DataGrid
of People
that I can delete People
from. After I delete a Person
I call the following method;
private void SetSelectedPerson(int personDeleted, int personID)
{
if (peopleDataGrid.SelectedIndex > 0)
{
peopleDataGrid.SelectedIndex--;
}
else
{
peopleDataGrid.SelectedIndex = 0;
}
}
This does select the previous Row
, however it removes the highlighting from the SelectedRow
. This is not what I want as although the SelectedIndex
is correct, it does not look like the Row
is selected to the user.
I do refresh the DataGrid
ItemSource
so that the deleted Person
is removed from the current Collection
. If I don't refresh the ItemSource
, the highlighting is correct but of course the deleted Person
is not removed from the DataGrid
.
I refresh the ItemSource
using;
People = await ReturnPeople();
PeopleICollectionView = CollectionViewSource.GetDefaultView(People);
peopleDataGrid.ItemsSource = PeopleICollectionView;
DataContext = this;
How can I keep the highlighting of the Row
on an ItemSource
update?
EDIT: For Sakura
This is how I Bind the DataGrid
if I remove the hard code of ItemSource
..
People = await ReturnPeople();
PeopleICollectionView = CollectionViewSource.GetDefaultView(People);
DataContext = this;
And in the 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}">
However when I delete a person it doesn't update in the DataGrid
...
After I delete a Person I call the following method...
Firstly, if your DataGrid
is single selected mode, when you delete selected item, the selected index will be set to -1. if it is multi selected mode, it will return index of one other selected item or -1 if don't have any. So you have to get the selected index of datagrid before you delete row. Then use this value after you delete.
Second, if your datagrid is binded correctly, you don't have to refresh the datacontext to make it update.
If you interested in fix this, post your binding code here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.