This is a migrated thread and some comments may be shown as answers.

RowStyleSelector when using ObservableCollection

4 Answers 126 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Comprima
Top achievements
Rank 1
Comprima asked on 05 Feb 2015, 03:58 PM
Hi,

I am trying to use RowStyleSelector with a RadGridView having an ItemsSource of type ObservableCollection<T>. The RowStyleSelector looks at the value of a property of T to set the background color of the corresponding row.

My problem is that the items may be altered outside of the grid. The new values are immediately picked up by the cells of the grid (because of the ObservableCollection and because T implements INotifyPropertyChanged), but the row style is not updated accordingly. However, if I sort the grid by clicking on one of the columns, the row styles are updated.

How can I ensure that the style is updated as soon as the values of the cells are updated?

In case it matters, this is how the RadGridView is declared (stripped down a bit):
<telerik:RadGridView Name="DocumentGrid" SelectionMode="Extended" ItemsSource="{Binding Path=Documents}" AutoGenerateColumns="False" IsReadOnly="True" RowStyleSelector="{StaticResource DocumentStyleSelector}">

This is the RowStyleSelector (stripped down a bit):
internal class DocumentStyleSelector : StyleSelector
{
    public Style DocumentModifiedStyle { get; set; }
 
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var document = item as Document;
        if (document != null && document.EntityState == EntityState.Modified)
        {
            return DocumentModifiedStyle;
        }
        return null;
    }
}

And this is the Style (stripped down a b
<layout:DocumentStyleSelector x:Key="DocumentStyleSelector">
    <layout:DocumentStyleSelector.DocumentModifiedStyle>
        <Style TargetType="telerik:GridViewRow">
            <Setter Property="Background" Value="DarkGray" />
        </Style>
    </layout:DocumentStyleSelector.DocumentModifiedStyle>
</layout:DocumentStyleSelector>


Best regards
Linus

4 Answers, 1 is accepted

Sort by
0
Comprima
Top achievements
Rank 1
answered on 06 Feb 2015, 02:09 PM
I managed to figure out some more details about this. I add columns to the grid manually. Some of the columns are for properties of the Document class. The Document has an ObservableCollection<Field> and some of the columns are for the Field.Value property. For example, the columns could be something like this:

[Document Id], [Document Number], [Document Name], [Field 1 Value], [Field 2 Value], [Field 3 Value], [Field 4 Value]

Both Document and Field is in an ObservableCollection<T> and both Document and Field implements INotifyPropertyChanged. It seems though as if the grid only picks up the changes in Document.

One way around this problem is to have Document listen to the PropertyChanged event of all the Fields and in turn raise its own PropertyChanged. This lets the grid understand something has changed.

Question remains: is this the expected behavior for the grid? Are there any other, better, ways of dealing with it?
0
Boris
Telerik team
answered on 10 Feb 2015, 12:19 PM
Hello Linus,

I tried to reproduce the mentioned behavior, however I was unsuccessful. Could you please examine the attached sample project for any differences with your own implementation? 

In order to investigate further why this behavior occurs could you please edit the attached sample project, in a way that it reproduces the issue? By doing so we will be able to debug it on our side and inform you of our findings. In addition, if you are not entirely sure how to isolate it, you can check this Isolating a problem in a sample project  blog post for some guidance.

Regards,
Boris
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Comprima
Top achievements
Rank 1
answered on 05 Mar 2015, 10:06 AM
Hi, I had a quick look at you sample project, and it does not look like how I described my project. Most notably, Club does not have a collection property that is databound to the grid.

If you modify your project and add a ObservableCollection<ExtraInfo> to Club, and databind the grid to the elements in this list (in such a way that every ExtraInfo gets a column of its own in the grid), then apply styling based on the value of any one of the ExtraInfo in the list, you will see that the styling is not applied when the ExtraInfo is altered in code behind.
0
Dimitrina
Telerik team
answered on 06 Mar 2015, 11:37 AM
Hi Linus,

I updated the solution so that the Club to have an additional collection property. If I understand you right, you are updating a property's value in the nested collection. In that case, the RowStyleSelector of the parent GridView bound to Clubs objects will not be reevaluated until the collection is rebound. This happens as you sort as column or invoke RadGridView.Rebind(). I am afraid it will not automatically reevaluate when updating such nested properties. 

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Comprima
Top achievements
Rank 1
Answers by
Comprima
Top achievements
Rank 1
Boris
Telerik team
Dimitrina
Telerik team
Share this question
or