Hello.
Is there any way to change the background color of the entire row of a selected cell when SelectionUnit="Cell"?
Thank you.
I made the wrong choice of controls. I am using SfDataGrid.
I have illustrated the requirements. I want to select cell by cell and I want to highlight the row that is being selected.
|
<Application.Resources>
<local:SelectorClass x:Key="styleSelector"/>
<Style x:Key="highlightCell" TargetType="syncfusion:GridCell">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Yellow" Opacity="0.5" />
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
<syncfusion:SfDataGrid x:Name="sfDataGrid"
SelectionUnit="Cell"
SelectionMode="Extended"
MouseUp="OnMouseUp"
KeyUp="OnKeyUp"
CellStyleSelector="{StaticResource styleSelector}"
AllowEditing="True"
ItemsSource="{Binding Orders}"
AutoGenerateColumns="False">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridTextColumn MappingName="OrderID" HeaderText="Order ID" />
<syncfusion:GridTextColumn MappingName="CustomerID" HeaderText="Customer ID" />
<syncfusion:GridTextColumn MappingName="CustomerName" HeaderText="Customer Name" />
<syncfusion:GridTextColumn MappingName="Country" HeaderText="Country" />
<syncfusion:GridTextColumn MappingName="UnitPrice" HeaderText="Unit Price" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid> |
|
private void HighlightRow(SfDataGrid sfDataGrid)
{
foreach (var rowIndex in visibleRowIndex)
{
//Change the CellStyle at runtime by based on row index in SfDataGrid.
sfDataGrid.UpdateDataRow(rowIndex);
}
//previously maintained rowindex cleared
visibleRowIndex.Clear();
foreach (var cell in sfDataGrid.GetSelectedCells())
{
//get the selected cells RowIndex
var rowIndex = sfDataGrid.ResolveToRowIndex((cell as GridCellInfo).RowData);
//Update the CellStyle at runtime baaed on selected cells RowIndex in SfDataGrid.
sfDataGrid.UpdateDataRow(rowIndex);
//Maintain RowIndex for change the CellStyle while removing and adding selection in SfDataGrid
visibleRowIndex.Add(rowIndex);
}
}
List<int> visibleRowIndex = new List<int>();
private void OnMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//get the SfDataGrid
SfDataGrid sfDataGrid = sender as SfDataGrid;
HighlightRow(sfDataGrid);
}
private void OnKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
//get the SfDataGrid
SfDataGrid sfDataGrid = sender as SfDataGrid;
HighlightRow(sfDataGrid);
} |
|
public class SelectorClass : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var column = (container as GridCell).ColumnBase.GridColumn;
var sfDataGrid = column.GetType().GetProperty("DataGrid", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(column) as SfDataGrid;
foreach (var cell in sfDataGrid.GetSelectedCells())
{
if ((cell as GridCellInfo).RowData.Equals(item))
return App.Current.Resources["highlightCell"] as Style;
}
return base.SelectStyle(item, container);
}
} |