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

StyleSelector does not get called GridRow updated

10 Answers 552 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 24 Sep 2010, 05:20 AM
Hi,

I have implemented a StyleSelector that applies a style based on a data condition for the ItemSource.
However this only works when a new record is inserted into the grid which seems to call the RowLoaded event.

I want to be able to apply a style to a row based on this ItemSource data value when a row is edited. Our DataContext.Load method is calling LoadBehaviour.MergeIntoCurrent for both inserted and updated records. Only the Inserted records seem to run the StyleSelector.

So does anyone know how to select an individual row in a grid and apply a style to it - even if no grid event has fired. In our system some other Composite event in our UI fires instead - and we have the Id of the record - but no reference to the Grid row. 

Or does someone know how to get the StyleSelector to work when a bound value in a row has been changed externally by another view. We have 2 views bound to the same RIA services domain context. One view contains a GridView which is bound to DomainContext.A and the other view contains a detail view which is bound to the same DomainContext.A. The detail view updates a record in DomainContext.A and the changes are reflected in the grid - but the StyleSelector does not get called.

Thanks

10 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 24 Sep 2010, 07:34 AM
Hello Simon,

You may take a look at this forum thread for a reference.
 

Sincerely yours,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jonx
Top achievements
Rank 2
answered on 23 Nov 2010, 10:39 PM
Hello Maya,
I have the same problem but the thread you recommend does not help...
Or I'm unable to make it work...
They are several other thread requesting a solution for that problem and none has a usable solution.
StyleSelector does only work for new rows and when I use virtualisation the style seems to be applied randomly.
I guess because it depends where the existing row gets reused in the virtualisation...

Also calling rebind is not an option because it is slow and because it does flicker once when everything is rebuilt.

Would you be kind enought and provide a working demo on how to change the style of a row based of the row cells content?
The style should also update if the bound underlying data changes.

Here is a scenario I try to implement...
I have a small grid that displays a list of adresses. The grid has an external DetailsPresenter to display the full details of the selected address. The addresses can be disabled throught an IsActive property. That property is visible in a check box column in the grid but also as a check box in the DetailsPresenter.

What I want is that when I uncheck the box in the grid, the row background changes to grey. I also want the row background turn to grey when I uncheck the check box in the related presenter. As both controls are bound to the IsActive property this should not be a problem. Of course, virtualisation should be on as without my grid is too slow...

So... would you please show me an example of how this can be achieved?
That would be really nice,
Thanks a lot in advance,
John.
0
Maya
Telerik team
answered on 24 Nov 2010, 08:48 AM
Hi John,

What is the version of RadGridView that you are using ? Did you try it out with our official Q3 2010 release ?  

Regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Jonx
Top achievements
Rank 2
answered on 24 Nov 2010, 05:23 PM
Ha that was my problem... I had some trouble with my dlls since I updated to Q3 and you are right my project was not updated...

And then indeed, your StyleSelector is working... It's even working when you update the bound data: perfect. Thanks a lot exactly what I needed.

Here is what I have...
public class MyStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var style = new Style(typeof(GridViewRow));
 
        var row = (GridViewRow)container;
        var obj = (Beneficiary)item;
 
        if (obj.IsActive.HasValue && obj.IsActive.Value == false)
        {
            style.Setters.Add(new Setter(GridViewRow.BackgroundProperty, new SolidColorBrush(Colors.Red)));
        }
        else
        {
            style.Setters.Add(new Setter(GridViewRow.BackgroundProperty, new SolidColorBrush(Colors.Blue)));
        }
 
        return style;
    }
}

But then I allso tried to make it work by code... I have the following. It is working at row creation and row edit... but now when I update the underlying data. Which event should I handle to update the row when the bound data changes? This is just out of curiosity as the styleselector stuff is working as expected on Q3...
private void grdBeneficiaire_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{
    UpdateRowStyle(e.DataElement as Beneficiary, e.Row as GridViewRow);
}
 
private static void UpdateRowStyle(Beneficiary benef, GridViewRow row)
{
    if (row != null && benef != null)
    {
        if (benef.IsActive.HasValue && benef.IsActive.Value == false)
        {
            row.Background = null;
            row.Background = new SolidColorBrush(Colors.Gray);
        }
    }
}
 
private void grdBeneficiaire_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
    UpdateRowStyle(e.EditedItem as Beneficiary, e.Row);
}

Thanks a lot for your help,
John.
0
Maya
Telerik team
answered on 25 Nov 2010, 11:28 AM
Hello John,

The StyleSelector is called upon changing the value of the particular property. Consequently, neither RowLoaded (that is fired only when a row is loaded) and RowEditEnded (that is fired after the items has been edited) are proper in this case.
Still, I am glad that your issue has been resolved.

All the best,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Jonx
Top achievements
Rank 2
answered on 25 Nov 2010, 11:50 AM
Yes, I'm glad too ;)

So StyleSelector is the way to go. No problem, it's working great.

One last question: how can I get the default row style and apply it to my row before modifying it to suit my needs?

Thank you for your help... It helped a lot...

John.
0
Maya
Telerik team
answered on 25 Nov 2010, 12:12 PM
Hello John,

You may take a look at our online documentation for detailed information.
  

Regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Jonx
Top achievements
Rank 2
answered on 25 Nov 2010, 12:21 PM
I'll do that, thank you...
0
Morgan McCollough
Top achievements
Rank 1
answered on 11 Apr 2011, 06:47 PM
I have been using row style selectors and cell style selectors to great effect now that they work in conjunction with property changes in the bound items (2011.1.405.1040).

However, I did find a minor issue. It seems that the style selection only happens if the property change event is on base list type that is bound to the grid. They do NOT get called if the property change is on another related object.

For example, if you have 2 entities: Employee and Department, and you are displaying a grid of employee information, you might have two columns defined as follows (using the navigation properties on Employee).

<telerik:RadGridView
     AutoGenerateColumns="False"
     ItemsSource="{Binding Employees}"
     Style="{StaticResource GenericGridViewStyle}"
     RowStyleSelector="{StaticResource EmployeeRowStyleSelector}"
     RowIndicatorVisibility="Collapsed"
     SelectionUnit="FullRow">
     <telerik:RadGridView.Columns>
       <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=EmployeeID}" IsReadOnly="True" Header="{Binding Path=GridStrings.EmployeeColumnID, Source={StaticResource ResourceWrapper}}" TextAlignment="Right"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=Name}"
           CellStyleSelector="{StaticResource EmployeeNameCellStyleSelector}"
            Header="{Binding Path=GridStrings.EmployeeColumnName, Source={StaticResource ResourceWrapper}}" TextAlignment="Right"/>
         <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=Department.Name}"
            CellStyleSelector="{StaticResource EmployeeDeptCellStyleSelector}"
            Header="{Binding Path=GridStrings.EmployeeColumnDepartment, Source={StaticResource ResourceWrapper}}" TextAlignment="Right"/>
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

It appears that the style selectors will not get called in this circumstance if the "Name" property on department changes even though it is bound to a column.

In my particular situation I just worked around it by raising an innocuous property change event on the parent object when something in the child changes, but I thought someone might want to know. Should I enter a bug on this, or was this by design?
0
Maya
Telerik team
answered on 14 Apr 2011, 01:35 PM
Hello Morgan McCollough,

Indeed, you are quite correct. We have logged the issue in our Public Issue Tracking System and you may follow its progress here.
Thank you for the cooperation. I have updated your Telerik points accordingly.
 

All the best,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Simon
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jonx
Top achievements
Rank 2
Morgan McCollough
Top achievements
Rank 1
Share this question
or