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
I would recommend you to use CellStyleSelector instead. Please take a look at our online documentation and demos for a reference.
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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
>
</ns
i:OrderCountStyle
>
So, what am I doing wrong?
Thanks again!
Regards,
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 >>
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
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
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
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
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 ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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
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 >>
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
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