Hi there, I'm just starting with first rehearsing with your controls and WPF, and I would like to change the color (or any other property) of the text displayed in a bound data column, using a simple ValueConverter that return a Black/Red SolidColorBrush based on the passed value:
and then in the XAML use an expression like this:
Is this possible ? Or what other way to keep it at simplest ?
regards
Ubaldo
                                MyPublic Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, _                         ByVal culture As Globalization.CultureInfo) As Object _                       Implements IValueConverter.Convert   Dim myColor As New SolidColorBrush(System.Windows.Media.Colors.Black)   Try     Dim cur As Decimal = CDec(value)     If cur < 0 Then myColor = New SolidColorBrush(System.Windows.Media.Colors.Red)   Catch ex As Exception   End Try   Return myColor End Functionxxxx.ForecolorPropertyName={Binding Path=DataColumnName, Converter=MyClassConverter}regards
Ubaldo
5 Answers, 1 is accepted
0
                                Hello Ubaldo,
In this case the style will be applied to all the cells in the grid. However, you may define the Style explicitly:
 
 
You may take a look at our online documentation for further reference. Greetings,
Maya
the Telerik team
                                        The easiest way would be to create a Style targeting the GridViewCell:
<Window.Resources>          <Style TargetType="telerik:GridViewCell">        <Setter Property="Foreground" Value="HotPink"/>    </Style>    </Window.Resources>In this case the style will be applied to all the cells in the grid. However, you may define the Style explicitly:
<Window.Resources>    <Style x:Key="MyCellStyle" TargetType="telerik:GridViewCell">        <Setter Property="Foreground" Value="HotPink"/>    </Style>    </Window.Resources><telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"            CellStyle="{StaticResource MyCellStyle}"/>You may take a look at our online documentation for further reference. Greetings,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it.  Learn more>> 
0
                                
                                                    Ubaldo
                                                    
                                            
    Top achievements
    
            
                
                Rank 1
            
    
                                                
                                                answered on 12 Jan 2011, 02:27 PM
                                            
                                        Hi Maya, thanks for your quick reply.
Probably I don't get very well the concept, due to my inexperience, but what I'm trying to do is to have the fore color of the text that changes in relation to the value of the text. Is less than zero, the color should be Red, otherwise Black.
I know that the quickest way to achieve this in wpf is to create a ValueConverter class that return the color, so that in the binding expression of the value you can add the Converter= parameter that takes is value from the custom converter class.
You suggest to use Styles... so do u mean that I should use a style with a trigger inside ? Well, for sure less straightforward than the Converter used directly in a binding expression.
I would really appreciate a complete sample (easy goal: render the text of a cell in Red if the value of that text is less than zero, otherwise standard Black)
TIA
Ubaldo
                                        Probably I don't get very well the concept, due to my inexperience, but what I'm trying to do is to have the fore color of the text that changes in relation to the value of the text. Is less than zero, the color should be Red, otherwise Black.
I know that the quickest way to achieve this in wpf is to create a ValueConverter class that return the color, so that in the binding expression of the value you can add the Converter= parameter that takes is value from the custom converter class.
You suggest to use Styles... so do u mean that I should use a style with a trigger inside ? Well, for sure less straightforward than the Converter used directly in a binding expression.
I would really appreciate a complete sample (easy goal: render the text of a cell in Red if the value of that text is less than zero, otherwise standard Black)
TIA
Ubaldo
0
                                Hi Ubaldo,
Maya
the Telerik team
                                        In this case you may use a CellStyleSelector as described in our online documentation and illustrated in our demos.
 
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it.  Learn more>> 
0
                                
                                                    Ubaldo
                                                    
                                            
    Top achievements
    
            
                
                Rank 1
            
    
                                                
                                                answered on 12 Jan 2011, 03:51 PM
                                            
                                        Ok, got it. It works, but with some issues that I would like to clarify. First, here the code (my GridView is bound to a CollectionViewSource generated by a standard ADO DataSet, and so returns a DataTable)  
I've tried to make the StyleSelector the more generic as possible, accepting a ColumnName parameter in order to select the column of which to test the value.
In the resources I've defined the two style colors, and the StyleSelector:
Now, in the GridWiev xaml, I've referenced the StyleSelector:
Q1: a lot of xaml for a simple task as this
Q2: if I want to apply this style selector to 20 different columns, should I define 20 different NumberColorStyle static resources so that each one of them can set the ColumnName ? How can I pass the ColumnName directly from the GridViewDataColumn tag ?
TIA
Ubaldo
                                        Public Class NumberColorStyle   Inherits StyleSelector     Public Property PositiveStyle() As Style   Public Property NegativeStyle() As Style   Public Property ColumnName() As String    Public Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style       If TypeOf item Is System.Data.DataRowView Then      Dim drow As System.Data.DataRowView = CType(item, System.Data.DataRowView)       If _ColumnName <> "" Then        Try          Dim cval As Decimal = CDec(drow.Item(_ColumnName))           If cval < 0 Then            Return NegativeStyle           Else            Return PositiveStyle           End If        Catch ex As Exception         End Try      End If    End If    Return Nothing    End Function  End ClassI've tried to make the StyleSelector the more generic as possible, accepting a ColumnName parameter in order to select the column of which to test the value.
In the resources I've defined the two style colors, and the StyleSelector:
<Style x:Key="ForeColorBlackStyle" TargetType="telerik:GridViewCell">   <Setter Property="Foreground" Value="Black"></Setter> </Style> <Style x:Key="ForeColorRedStyle" TargetType="telerik:GridViewCell">   <Setter Property="Foreground" Value="Red"></Setter> </Style> <local:NumberColorStyle x:Key="NumberColorStyle" ColumnName="importo"                         PositiveStyle="{StaticResource ForeColorBlackStyle}"                         NegativeStyle="{StaticResource ForeColorRedStyle}"></local:NumberColorStyle> <telerik:GridViewDataColumn Header="Importo (EUR)" DataMemberBinding="{Binding importo}"                             UniqueName="importo" DataFormatString="{} {0:C}"                             TextAlignment="Right"                             CellStyleSelector="{StaticResource NumberColorStyle}"> Q1: a lot of xaml for a simple task as this
Q2: if I want to apply this style selector to 20 different columns, should I define 20 different NumberColorStyle static resources so that each one of them can set the ColumnName ? How can I pass the ColumnName directly from the GridViewDataColumn tag ?
TIA
Ubaldo
0
                                Accepted
Hello Ubaldo,
Once you get the myColumn variable, you may get its UniqueName property.
Regards,
Maya
the Telerik team
                                        Generally, when you have a lot of items and columns defined, using a IValueConverter is not an optimized solution as it may lead to some performance issues. As for taking the right column in the StyleSelector class, you may use the container argument which will be of type GridViewCell and find the column it belongs to by its Column property:
var cell = container as GridViewCell;var myColumn = cell.Column as GridViewDataColumn;Regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it.  Learn more>>