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

Setting GridViewCell Background color depending on content

12 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 24 Nov 2011, 05:11 PM
Hi,

I want to change the background color of a GridViewCell depending on content. I defined my style on the following way:
<Style x:Key="MyStyle" TargetType="telerik:GridViewCell">
        <Setter Property="Background" Value="{Binding Converter={StaticResource ValueToColorConverter}}"/>
 </Style>

I defined this style in my app.xaml, because my gridview has autogenerated columns and I have to set my cellstyle programmatically.

When I try to run my application, I get the following error:
{System.Windows.Markup.XamlParseException: Set property '' threw an exception. [Line: 19 Position: 36] ---> System.NotSupportedException: Cannot set read-only property ''.
   at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
   at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)

When I define my style in the following way:
<Style x:Key="myStyle" TargetType="telerik:GridViewCell">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="{Binding Converter={StaticResource ValueToColorConverter}}" />
        </Setter.Value>
    </Setter>
</Style>

my application runs, but the converter is never been called.

What am I doing wrong? And what's the difference between both the style definitions?

Thanks in advance.

Best regards,
Peter
    

12 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 24 Nov 2011, 05:14 PM
Hello Peter,

I would recommend you to use CellStyleSelector instead. Please take a look at our online documentation and demos for a reference.
 

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 24 Nov 2011, 06:42 PM
Hello Maya,

I also tried that before (see code, I already found your styleselector approach on your website :)), but the same problem while parsing the xml (readonly error).
<nsi:CountStyle x:Key="orderCountStyle">
        <nsi:CountStyle.BelowZeroStyle>
            <Style TargetType="telerik:GridViewCell">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="{Binding Converter={StaticResource ValueToColorConverter}}" />
                    </Setter.Value>
                </Setter>
            </Style>
        </nsi:CountStyle.BelowZeroStyle>
        <nsi:CountStyle.NormalStyle>
            <Style TargetType="telerik:GridViewCell">
                <Setter Property="Background" Value="{StaticResource over}" />
            </Style>
        </nsi:CountStyle.NormalStyle>
    </nsi:OrderCountStyle>

So, what am I doing wrong?

Thanks again!

Regards,
Peter


0
Maya
Telerik team
answered on 25 Nov 2011, 07:43 AM
Hello Peter,

Actually, you cannot use bindings in the setters of a style in Silverlight 4.  That is why you need to define the background as a constant value:

<Setter Property="Background" Value="Red" />


Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 25 Nov 2011, 08:49 AM
Hi Maya,

Ok, I understand. But I still need a ValueToColorConverter for my background :). Is there another way to realize that? Or is that not possible at all?

Regards,
Peter
0
Atif
Top achievements
Rank 1
answered on 26 Nov 2011, 06:01 AM
Hi Maya,
Can you help me with this problem? its a headache for me and no one is replying to my thread.

http://www.telerik.com/community/forums/silverlight/gridview/silverlight5-issue-in-cell-loaded-event.aspx
0
Peter
Top achievements
Rank 1
answered on 28 Nov 2011, 10:24 AM
Hi Maya,

I installed Silverlight 5 RC and now it is possible :).

The following snippet works:
<Style x:Key="myStyle" TargetType="telerik:GridViewCell">
    <Setter Property="Background" Value="{Binding Converter={StaticResource ValueToColorConverter}}" />           
</Style>

But... it passes the whole row to the converter, and not only my cell value (I can pass a columnname as binding parameter, but that's not what I want). How can I fix that?

Thanks again!

Regards,
Peter
0
Peter
Top achievements
Rank 1
answered on 30 Nov 2011, 12:10 PM
Hi,

When I use a Styleselector, there is 2nd param in the SelectStyle function "DependencyObject" which is my actual GridViewCell. Is it maybe possible to pass that object to my converter through xaml? Or is it way easier?

Regards,
Peter 

 

0
Maya
Telerik team
answered on 01 Dec 2011, 01:13 PM
Hello Peter,

Could you clarify a bit the exact scenario that you want to accomplish ? Why do you need a converter ? Is CellStyleSelector not appropriate for you ?
 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 01 Dec 2011, 01:19 PM
Hi Maya,

depending of the value of the cell I want to calculate the brush opacity. So for example when you get a larger number, the opacity will grow (till max. 1).

Regards,
Peter
0
Maya
Telerik team
answered on 01 Dec 2011, 02:07 PM
Hi Peter,

The 'value' parameter in the converter will be the corresponding item. So, if the style selector is defined for a specific column - let us say - StatiumCapacity, you can take its value as follows:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Club currentClub = value as Club;
            if (currentClub.StadiumCapacity < 10000)
            {
                return new SolidColorBrush(Colors.SpringGreen);
            }
            else
            {
                return new SolidColorBrush(Colors.Ivory);
 
            }
        }

Will that approach suit your needs ? 
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Peter
Top achievements
Rank 1
answered on 01 Dec 2011, 02:34 PM
Hi Maya,

I already tried this, but the whole row is passed as an object in my converter. How can I determine which is the current cell from the row?

Thanks again!
Peter
 
0
Peter
Top achievements
Rank 1
answered on 06 Dec 2011, 09:58 AM
Hi,

Summary of this thread, see below.

I want to use the gridviewcell value to calculate the brush opacity of the cell.
I'm using a converter which I bind on the following way:

<Style x:Key="myStyle" TargetType="telerik:GridViewCell">
<Setter Property="Background" Value="{Binding Converter={StaticResource ValueToColorConverter}}" />
</Style>

This is my converter:
Public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//here the "value" object contains the whole row. I just want the cell value. How can i achieve that?
}

See the comment in my converter. Is it possible the achieve this?

Regards,
Peter
Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Maya
Telerik team
Peter
Top achievements
Rank 1
Atif
Top achievements
Rank 1
Share this question
or