I have a very simple grid setup with the following XAML:
<igDP:XamDataGrid Grid.Row="0" Name="grid" igEditors:ValueEditor.ValueChanged="ValueChanged" Height="80"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False" /> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:Field Name="Name" Label="Name" /> <igDP:Field Name="Last" Label="Last" /> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid>
Simple enough, right? I catch the ValueEditor.ValueChanged event. The idea is that I can then get the ValueEditor's Host (which is a ValuePresenter) and I could change the background color of the CellValuePresenter.
However, if it seems that by scrolling the grid, the ValueChanged event is raised and it passes Value = null. This is really strange and, for my purposes, makes no sense.
I could catch the CellUpdated event on the grid, but I don't know how to get the CellValuePresenter from an instance of a Cell.
Thanks,
- Szymon
I believe this is a side-effect from the cell virtualization feature. Cell virtualization tries to reuse the existing cell value presenters and their ValueEditors. Becuase the code automatically resets the values inside the ValueEditors to match the current rows, ValueChanged is raised. I suppose that this is more of a feature than a bug, since in a way its behavior that is defined, but... this is quite annoying and unexpected.
Worse of all, setting a field's AllowCellVirtualization setting to False, doesn't solve the issue.
-Szymon
Hi Szymon,
Yes, you are correct - ValueEditor.ValueChanged will get raised as value editors are reused.
As for the question about how to get CellValuePresenter from a Cell, for that you can use the CellValuePresenter's FromCell and FromRecordAndField static methods to get the cvp for a cell.
Regarding the original issue about how to specify background based on the value - for this you can bind the Background property of CellValuePresenter to its Value property and specify a converter on the binding to a IValueConverter implementation that converts value to color. Something along the lines of the following:
<Window.Resources> <!--Converter that converts a value to color. See code in ValueToColorConverter class.--> <local:ValueToColorConverter x:Key="colorConverter" /> <!--Style that applies the converter to Background property of the CellValuePresenter.--> <Style x:Key="{x:Type dg:CellValuePresenter}" TargetType="{x:Type dg:CellValuePresenter}" > <Setter Property="Background" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Value, Converter={StaticResource colorConverter} }" /> </Style></Window.Resources>
Hope this helps,
Sandip