Hi,
I am facing a performance issue in XamDataGrid.
According to my requirement grid BackgroundSelectedProperty and BackgroundHoverProperty should be green initially
Based on some condition i am trying to change the color to one of selectedrecords to lightcyan in grid.
private void HighlightRecord(XamDataGrid grid, string cid){if (grid == null)return;Style lightCyanStyle = new Style(typeof(DataRecordCellArea));lightCyanStyle.Setters.Add(new Setter(DataRecordCellArea.BackgroundHoverProperty, Brushes.LightCyan));lightCyanStyle.Setters.Add(new Setter(DataRecordCellArea.BackgroundSelectedProperty, Brushes.LightCyan));if (grid.Records != null){foreach (var eachRecord in grid.SelectedRecords){var record = eachRecord as DataRecord;if (record != null && ((DataRowView)(record).DataItem).Row.Table.Columns.Contains("COLUMN")){var id = ((DataRowView)(record).DataItem).Row["COLUMN"];if (cid == Convert.ToString(id, CultureInfo.InvariantCulture)){RecordPresenter recordPresenter = RecordPresenter.FromRecord(record);DataRecordCellArea cellArea =Infragistics.Windows.Utilities.GetDescendantFromType(RecordPresenter.FromRecord(record),typeof(DataRecordCellArea), true) as DataRecordCellArea;if (recordPresenter != null){if (cellArea != null)cellArea.Style = lightCyanStyle;}break;}}}}}
Once after Deselection I am trying to reset my grid BackgroundSelectedProperty and BackgroundHoverProperty to green
To Achieve this i tried using below code but it is not working
grid.FieldLayoutSettings.DataRecordCellAreaStyle = GreenStyle;
So i am trying to iterate for eachrecord and applying my styles back which is causing a performance issue.
private void ResetGridRecordColor(XamDataGrid grid){if (grid?.Records != null && grid.Records.Count > 0){
Style GreenStyle = new Style(typeof(DataRecordCellArea));GreenStyle.Setters.Add(new Setter(DataRecordCellArea.BackgroundHoverProperty, Brushes.Green));GreenStyle.Setters.Add(new Setter(DataRecordCellArea.BackgroundSelectedProperty, Brushes.Green));
foreach (var eachRecord in grid.Records){var record = eachRecord as DataRecord;var recordPresenter = RecordPresenter.FromRecord(record);if (record != null && RecordPresenter.FromRecord(record) != null){DataRecordCellArea cellArea =Infragistics.Windows.Utilities.GetDescendantFromType(RecordPresenter.FromRecord(record),typeof(DataRecordCellArea), true) as DataRecordCellArea;if (recordPresenter != null && cellArea != null){cellArea.Style = GreenStyle;}}}}}
Could you please provide me a solution for this problem
Hello,
My recommendation is to use a trigger for this rather than attempting to manually apply and remove the cyan style. You can add a trigger to the xamDataGrid's style which will set the hover and selected color to cyan when your condition is met.
You can read more about Triggers here: https://msdn.microsoft.com/en-us/library/system.windows.style.triggers(v=vs.110).aspx
Please let me know if you have any further questions.
Hi Mike,
Initially i have tried using triggers only. But it is not working.
<Style x:Key="Highlight" TargetType="{x:Type igDP:DataRecordCellArea}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsActive}" Value="True"> <Setter Property="BackgroundActive" Value="LightCyan" /> </DataTrigger> <DataTrigger Binding="{Binding Path=IsSelected}" Value="True"> <Setter Property="BackgroundHover" Value="#CCE6F7" /> <Setter Property="BackgroundSelected" Value="#CCE6F7" /> </DataTrigger> </Style.Triggers> </Style>
If I am understanding your use case correctly, you want the selected background color to be Cyan when certain conditions are met and another color if those same conditions are not met. In order to accomplish this, you should bind the trigger to a property that is true under the correct condition and false otherwise. In the below sample, I have used a checkbox's IsChecked property so it's easy to toggle for testing purposes. Depending on the exact conditions under which you want to apply the Cyan color, you will bind to a different trigger.
<igDP:XamDataGrid Name="xamDataGrid" DataSource="{Binding Source={StaticResource odpObjectDataProvider1}, Path=Cars}"> <igDP:XamDataGrid.Resources> <Style TargetType="{x:Type igDP:DataRecordCellArea }"> <Setter Property="BackgroundSelected" Value="Green"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=styleCheckBox, Path=IsChecked}" Value="True"> <Setter Property="BackgroundSelected" Value="Cyan"/> </DataTrigger> </Style.Triggers> </Style> </igDP:XamDataGrid.Resources> </igDP:XamDataGrid>
Please try this out and let me know whether it works for you.