I'm working with VS2015 on a WPF application. On one of my WPF windows, I got a DataGrid with SelectionUnit = Cell and SelectionMode = Single. Furter I got a method to move rows inside the DataGrid up and down for sorting. The sorting works but the problem is that the last cell which was selected by the mouse cursor is visually always selected (blue background), which could disturb the user. So i tried to remove that visual marking of the cell by the following code lines:
datagrid.UnselectAllCells();
datagrid.SelectedCells.Clear();
None of the two lines works for me. The last selected cell is still selected.
How can I remove that selection?
Any help would be appreciated.
At last a snippet from the XAML with the definition of the DataGrid:
<DataGrid x:Name="grdGraphicalElementMatrix" Grid.Row="1" Grid.Column="0"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
CanUserAddRows="True"
IsReadOnly="False"
AutoGenerateColumns="False"
SelectionUnit="Cell" SelectionMode="Single"
CurrentCellChanged="grdGraphicalElementMatrix_CurrentCellChanged"
ItemsSource="{Binding GraphElemMatrix}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="colXAssignment"
Width="1*"
Binding="{Binding Path=X}"
Header="X"/>
<DataGridTextColumn x:Name="colYAssignment"
Width="1*"
Binding="{Binding Path=Y}"
Header="Y"/>
</DataGrid.Columns>
</DataGrid>
grdGraphicalElementMatrix_CurrentCellChanged is a method in which i can get the selected row and column when the user clicked in one of the cells to select it.
private void grdGraphicalElementMatrix_CurrentCellChanged(object sender, EventArgs e)
{
if (grdGraphicalElementMatrix.CurrentCell != null && grdGraphicalElementMatrix.CurrentCell.Column != null)
{
vm.GrdGraphicalElementMatrixSelColIndex = grdGraphicalElementMatrix.CurrentCell.Column.DisplayIndex;
vm.GrdGraphicalElementMatrixSelRowIndex = grdGraphicalElementMatrix.Items.IndexOf(grdGraphicalElementMatrix.CurrentItem);
}
}