I have defined a RowStyleSelector which sets the background color of a row based on the value of a property in the data object underlying the row. It works great when the SfDataGrid is initially populated.
However, I need to be able to "re-trigger" the style selection when a property in that data object changes at runtime (it's an observable property which raises the INotifyPropertyChanged event). Unfortunately, I haven't been able to figure out a way to do that.
I suppose, if nothing else is available, I could simply clear and recreate the list of data objects. But that seems kind of kludgy to me.
Is there a way to trigger RowStyleSelector when the underlying data changes?
Hi Mark,
We are still working on this. We will update you with further details on or before March 16, 2022.
We appreciate your patience and understanding.
Regards,
Vijayarasan S
|
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
ResourceDictionary.MergedDictionaries>
<local:CustomRowStyleSelector x:Key="rowStyleSelector" />
<Style x:Key="rowStyle1" TargetType="syncfusion:DataGridRowControl">
<Setter Property="Background" Value="Bisque" />
Style>
<Style x:Key="rowStyle2" TargetType="syncfusion:DataGridRowControl">
<Setter Property="Background" Value="Aqua" />
Style>
ResourceDictionary>
Application.Resources>
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
{
this.dataGrid.View.RecordPropertyChanged += OnRecordPropertyChanged;
}
private void OnRecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//Change the RowStyleSelector or CellStyleSelector at runtime by update the value changed row index in SfDataGrid
UpdateDataRow(index);
}
private void OnCurrentCellEndEdit(object sender, Syncfusion.UI.Xaml.DataGrid.CurrentCellEndEditEventArgs e)
{
index = e.RowColumnIndex.RowIndex;
//Change the RowStyleSelector or CellStyleSelector at runtime by update the value changed row index in SfDataGrid
UpdateDataRow(index);
}
public void UpdateDataRow(int index)
{
var visualcontainer = this.dataGrid.GetType().GetProperty("VisualContainer", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this.dataGrid) as VisualContainer;
if (visualcontainer == null)
return;
var datarow = this.dataGrid.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == index);
if (datarow != null)
{
var rowIndex = datarow.GetType().GetProperty("RowIndex", BindingFlags.Instance | BindingFlags.Public);
rowIndex.SetValue(datarow , -1);
visualcontainer.InvalidateMeasureInfo();
visualcontainer.InvalidateArrange();
}
}
|
|
public class CustomRowStyleSelector : StyleSelector
{
protected override Style SelectStyleCore(object item, DependencyObject container)
{
var row = (item as DataRowBase).RowData;
var data = row as ProductSalesDetails;
if (data.Quantity < 6)
return App.Current.Resources["rowStyle1"] as Style;
return App.Current.Resources["rowStyle2"] as Style;
}
} |