Reputation: 664
I microsof Excel, when a cell or a group of cells are selected, colums' headers and the rows' headers will be highlighted. How can I implement a similar feature in a wpd DataGrid?
I think I should handle DataGrid.SelectionChanged
event, but I have no idea how I can proceed. any help is appreciated.
Upvotes: 4
Views: 1406
Reputation: 19296
I think that the easiest way to do this is using SelectedCellsChanged event.
Check my example:
XAML code:
<DataGrid Name="myData"
AutoGenerateColumns="True"
SelectionMode="Extended"
SelectionUnit="Cell"
SelectedCellsChanged="myData_SelectedCellsChanged"
/>
Code-behind:
private void myData_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
foreach (var item in myData.Columns)
{
item.HeaderStyle = null;
}
if (myData.SelectedCells != null && myData.SelectedCells.Count != 0)
{
Style styleSelected = new Style();
styleSelected.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.Orange)));
foreach (var item in myData.SelectedCells)
{
item.Column.HeaderStyle = styleSelected;
}
}
}
You can also set Border.BorderBrushProperty and Border.BorderThicknessProperty in styleSelected if you want vertical line between columns.
Upvotes: 1