Hi. I'm trying to get row background color in the code:
DataRecordPresenter presenter = RecordPresenter.FromRecord(grid.Records[0]) as DataRecordPresenter;Color backgroundColor = ((SolidColorBrush) presenter.Background).Color;
This code returns color #00FFFFFF, but actual bakground color is #FFAFEEEE. It is set by the converter. The DataRecordCellArea style:
<Style x:Key="DataRecordCellAreaBaseStyle" TargetType="idp:DataRecordCellArea" BasedOn="{StaticResource TrackingGridRowDefaultStyle}"> <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.BuySell, Converter={conv:BuySellToBrushConverter}, ConverterParameter={x:Static conv:BuySellToBrushConverter.BackgroundParameter}}" /> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.BuySell, Converter={conv:BuySellToBrushConverter}, ConverterParameter={x:Static conv:BuySellToBrushConverter.ForegroundParameter}}" /> </Style>
Why I can't get correct background color?
Hello,
I have been looking into this and can see what is the trouble. You are colouring the DataRecordCellArea of the DataRecordPresenter and not the actual DataRecordPresenter you are accessing. What you can do is use the following code snippet:
DataRecordPresenter presenter = RecordPresenter.FromRecord(xamDataGrid1.Records[1]) as DataRecordPresenter;
DataRecordCellArea cellArea = Utilities.GetDescendantFromType(presenter, typeof(DataRecordCellArea), false) as DataRecordCellArea;
Color backgroundColor = ((SolidColorBrush)cellArea.Background).Color;
Please let me know if I can assist you any further on that matter.
Sincerely,
Petar Monov
Developer Support Engineer
Infragistics Bulgaria
www.infragistics.com/support
Petar, thanks for your answer. It works for me but I've found one issue: RecordPresenter.FromRecord returns data record presenter only for visible records (I assume this is because presenter are destroyed for records that are not currently in view). But I need to get background colors for all records in the grid. Is it possible somehow to do this?
Hi,
I see what you mean. This is caused by the virtualization of visual elements in the XamDataGrid and works as you suggested in order to provide a much greater performance. The XamDataGrid does expose functionality to control this. You can set the RecordContainerGenerationMode and CellContainerGenerationMode properties to Preload.
Please let me know if this did what you need.
Best regards Petar.
Petar, with setting RecordContainerGenerationMode to PreLoad it works as it should. But even with ~200 rows in the grid I see performance differences when scrolling grid. But in production there could be plenty of rows. I'm writing custom clipboard functionality so all grid rows could be copied to excel and I need to preserve cell colors. That's why I need to get rows background and foreground colors. So, is there any possibility to still have virtualization but to force DataRecordPresenter object to be created when clipboard operation will be invoked?
If you are going to need it only for an instance you can try setting the RecordGenerationMode to Preload before you copy your data and switch it back to Virtualize after you are done. If this works you will probably get a bit of a slow performance on the whole copying action.
Please test this out and let me know if it is any better. Looking forward to your reply.
Unfortunately, this method doesn't work. DataRecordPresenters aren't created when setting RecordContainerGenerationMode to PreLoad.
I see what you mean and usually such approaches are not recommended since the virtualizations is pretty useful for the XamDataGrid and there is no way to use the visual elements, the ones that are actually virtualized to stored any relative data. I assume you are using your color as a marker of some sort and the best approach I can suggest is simply adding a Brush or a Color field in your underlying data. You can bind your Visuals to it and access it directly through your DataItem and you can still use the virtualization provided with the grid. I strongly recommend using this approach, however if it is not usable for your scenario you can always use the Record’s Tag property to store this information like I have done in this forum thread: http://forums.infragistics.com/forums/t/48810.aspx , just that I have used the Cell class’ Tag property.
Hope this helps. Please let me know if you require any further assistance on the matter.