I want to manipulate selection in WPF datagrid, but I have problem with access to actual cells and setting focus on them and marking them as selected.
I wanted to make custom selection on WPF Datagrid when selecting more cells (one by one) without pressing Ctrl. I did it quite good but I'm having problems when I want to deselect one of selected cells - by just simply clicking on it. It's not a problem to remove it from the list. Problem is that When it's clicked on it takes focus and is hilighted and all others that were selected turn off their hilight. If I select another cell that wasn't selected all the selected cells get hilighted again correctly. The problem is only in the deselection.
XAML:
<Window x:Class="SelectionTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid
Name="mydatagrid"
Width="Auto" Height="Auto"
HeadersVisibility="All"
AutoGenerateColumns="True" IsReadOnly="True"
SelectionMode="Extended" SelectionUnit="Cell"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserReorderColumns="False" CanUserSortColumns="False"
SelectedCellsChanged="mydatagrid_SelectedCellsChanged"
Padding="10" HorizontalAlignment="Center" VerticalAlignment="Top"
>
</DataGrid>
</Grid>
</Window>
I have filled the datagrid with list of some random example class objects I made.
C#:
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
DataGridCellInfo cellInfo = new DataGridCellInfo(cell);
if ((cell.IsSelected)||(selectedList.Contains(cellInfo))||(selectedCellsList.Contains(cell)))
{
selectedList.Remove(cellInfo);
selectedCellsList.Remove(cell);
cell.IsSelected = false;
mydatagrid.CurrentCell = selectedList[0];
}
else
{
if (selectedList.Count < 7)
{
selectedList.Add(cellInfo);
selectedCellsList.Add(cell);
}
else
{
selectedList.RemoveAt(0);
selectedList.Add(cellInfo);
selectedCellsList.RemoveAt(0);
selectedCellsList.Add(cell);
}
}
mydatagrid.SelectedCells.Clear();
mydatagrid.UnselectAll();
foreach (DataGridCell xcell in selectedCellsList)
{
xcell.IsSelected = true;
xcell.Focus();
}
}
If this code looks really ugly to you, then I'm sorry. But I'm still just a little csharpawan.
What's my problem in shortcut: Clicking on selected cell makes only it hilighted and focused and dehilights all other selected cells which is exact opposite what I want it to do. (If I click other not yet selected cell it works the way I want it.)
Answer to question 1: A quick way to get DataGridCell from DataGridCellInfo:
public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent != null)
return (DataGridCell) cellContent.Parent;
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With