MultiBinding RelativeSource PreviousData for RadGridView

1 Answer 76 Views
Top achievements
Rank 1
Premier asked on 17 May 2023, 04:25 PM

Hi, i am converting our app datagrids to RadGridViews and we need to change the colour of the row based on the previous row data. We used a multibinding ad passed "PreviousData" as the relative source:

                    <Style TargetType="DataGridRow">
                            <DataTrigger Value="True" >
                                <DataTrigger.Binding >
                                    <MultiBinding Converter="{StaticResource EvaluatePreviousRow}">
                                        <Binding RelativeSource="{RelativeSource PreviousData}"/>
                                        <Binding Path="."/>
                                <Setter Property="Background" Value="Gray"/>
                        <Setter Property="Background" Value="Red"/>

Now i managed to convert everything so that the EvaluatePreviousRow converter is called when a row is being drawn by the RadGridView but now 

<Binding RelativeSource="{RelativeSource PreviousData}"/>

Always sends null to the converter. Is there any way to pass the previous collection item data in a similar fashion?

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 22 May 2023, 11:29 AM


Due to the UI virtualization feature of the RadGridView control, the suggested way for modifying the GridViewRow element (represents each row in the control) for such a scenario is via its RowStyleSelector property. The following article shows more information regarding it:

WPF DataGrid - RowStyleSelector - Telerik UI for WPF

Generally, a custom StyleSelector class would have to be created and its SelectStyle method should be overridden. The item parameter will be the underlying data object instance that will be displayed in the current GridViewRow element. Through the container parameter (it will be of type GridViewRow), using its GridViewDataControl property, you can retrieve the RadGridView instance from which the collection of data objects could be retrieved (either by casting its ItemsSource collection or by using its DataContext property). You could find the position of the current data object in this collection and find the one in the previous position and compare them. The data from the previous and current objects could be compared and different Style properties could be returned by the SelectStyle method. After that, create a new instance of the custom StyleSelector and define Style instances for each property that will be returned by the method. Finally, set the newly created instance of the StyleSelector to the RowStyleSelector property of RadGridView.

In addition, we have a runnable example that shows the RowStyleSelector functionality. It could be found in our SDK Samples Browser under GridView/RowStyleSelector, as well as its code could be retrieved from our GitHub repository.

With this being said, I hope the provided information will be of help to you. 

Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question