change the rowstyle foreground color based on a data value

7 posts, 0 answers
  1. Clinton van Ry
    Clinton van Ry avatar
    10 posts
    Member since:
    Feb 2009

    Posted 02 Oct 2009 Link to this post

    Hi,

    I am trying to change the  row style using a converter however if use the code below i get the wonderful error AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR from microsoft. If i use a literal value like "Green" it then works.

     

    <telerik:RadGridView.RowStyle> 
    <StaticResource ResourceKey="GridViewRowStyleTest"/>  
     <Style TargetType="telerikGridView:GridViewRow">  
      <Setter Property="Foreground" Value="{Binding Status,Converter={StaticResource  workingvideoStatusForeground}}"/>  
    </Style> 
    </telerik:RadGridView.RowStyle> 

     


    I have also tried this on a cellstyle and have the same results.

    <telerik:GridViewDataColumn Header="File" DataMemberPath="FileName" IsReadOnly="True">  
                                <telerik:GridViewColumn.CellStyle> 
                                    <Style TargetType="telerikGridView:GridViewCell">  
                                        <Setter Property="Foreground" Value="Blue"/> <!-- "{Binding Status,Converter={StaticResource workingvideoStatusForeground}}"></TextBlock> --> 
                                    </Style> 
                                </telerik:GridViewColumn.CellStyle> 
                            </telerik:GridViewDataColumn> 
     The converter is very simple and returns a solid brush

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
            {  
                SolidColorBrush brush = new SolidColorBrush(Colors.Black);  
                WorkingVideoStatusType wvStatusEnum = (WorkingVideoStatusType)System.Convert.ToInt32(value);  
                switch (wvStatusEnum)  
                {  
                    case WorkingVideoStatusType.Complete:  
                        {  
                            brush = new SolidColorBrush(Colors.Green);  
                            break;  
                        }  
                    case WorkingVideoStatusType.InProgress:  
                        {  
                            brush = new SolidColorBrush(Colors.Purple);  
                            break;  
                        }  
                    case WorkingVideoStatusType.Waiting:  
                        {  
                            brush = new SolidColorBrush(Colors.Gray);  
                            break;  
                        }  
                    case WorkingVideoStatusType.Failed:  
                        {  
                            brush = new SolidColorBrush(Colors.Red);  
                            break;  
                        }  
                }  
                return brush;  
            } 

    I really wish silverlight had datatriggers. I have also attempted another way by using a cell style and add the textbox to the control template which works but i would prefere to adjust the rowstyle

  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2032 posts
    Member since:
    Dec 2014

    Posted 03 Oct 2009 Link to this post

    Hello Clinton van Ry,

    I believe  problem is in the Silverlight platform. I have not yet found a way to directly use bindings in style setters in Silverlight. 

    So what you can actually do is  to place the converter in the GridViewCell template, or set the binding in code behind. Both approaches are demonstrated  here.  Please note that  using converter in the style setter is only possible in WPF .

    In case you have troubles implementing one of the two approaches demonstrated in the blog post , just let me know.

    Regards,
    Pavel Pavlov
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

  3. Download a free trial of UI for WPF
  4. Clinton van Ry
    Clinton van Ry avatar
    10 posts
    Member since:
    Feb 2009

    Posted 04 Oct 2009 Link to this post

    Hi Pavel,

    Hopefully in the next version of silverlight they will allow style setters for binding.

     

    Thank you for you quick reply and your blog, very interesting reading.

     

    I have tried both samples that you have provided but noticed some strange behaviour. The foreground is only set once and does not update if you change solid color brush. i.e. If you update the data with a timer for example. However the background does change and behaves as expected. I do prefer using the behaviour as way to go forward than 100 lines or more in xaml. In my books less is better.

     

    What i am trying to achieve is to set the foreground to (green, red, black) and fontweight (bold, normal) based on a data value i receive. The data value represents a system status which changes often.

     

    Kind Regards,

     

    Clinton

  5. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2032 posts
    Member since:
    Dec 2014

    Posted 07 Oct 2009 Link to this post

    Hi Clinton , 

    I have prepared a small sample to demonstrate how the ForeGround may respond to runtime changes in values. In the attached project when you click on a button , the value of the price cell changes and triggers  a respective change of the foreground color.
    Please note that I have implemented the INotifyPropertyChanged interface to the Product class. This allows RadGridView to listen for changes and update the UI automatically.

    Greetings,
    Pavel Pavlov
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

  6. Clinton van Ry
    Clinton van Ry avatar
    10 posts
    Member since:
    Feb 2009

    Posted 10 Oct 2009 Link to this post

    Hi Pavel Pavlov,

    Thank you for your comments. However this is not the problem i am using RIA Service which has the base entity class. The base entity class has the INotifyPropertyChanged. I have prepared a sample that demonstrates the problem.

     

    If you click on the grid scrollbar and scroll so that the row is not visible and scroll it again to make it visible the row cells change to the correct color - very strand as if a repaint is required.

     

    I will post a support ticket for you to have a look at it. The sample application is based on the sample app you have provided in this forum . i.e. product class with two properties productname, price

     

    Kind Regards

     

    Clinton

     

  7. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2032 posts
    Member since:
    Dec 2014

    Posted 12 Oct 2009 Link to this post

    Hello Clinton van Ry,

    Thanks for sending me your project.  So here is what I have found :

    The service returns a ReadOnlyObservableCollection and it is used as an items source for RadGridView. RadGridView would listen for INotifyPropertyChanged and INotifyCollectionChanged Events. However it seems on each timer tick we get a new instance of the collection, rather than updating the existing one and throwing an event.
    So the solution is to call the RadGridView.Rebind()  method , after the new collection comes from the service. I have tried it with your project and it seemed to solve the problem.  Please see  answer to your ticket for more details.

    All the best,
    Pavel Pavlov
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

  8. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2032 posts
    Member since:
    Dec 2014

    Posted 12 Oct 2009 Link to this post

    Hello Clinton van Ry,

    Thanks for sending me your project.  So here is what I have found :

    The service returns a ReadOnlyObservableCollection and it is used as an items source for RadGridView. RadGridView would listen for INotifyPropertyChanged and INotifyCollectionChanged Events. However it seems on each timer tick we get a new instance of the collection, rather than updating the existing one and throwing an event.
    So the solution is to call the RadGridView.Rebind()  method , after the new collection comes from the service. I have tried it with your project and it seemed to solve the problem.  Please see  answer to your ticket for more details.

    All the best,
    Pavel Pavlov
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

Back to Top
Download a free trial of UI for WPF