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

WPF GridView Convertor Problem

1 Answer 143 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dermot
Top achievements
Rank 1
Dermot asked on 29 Apr 2011, 03:55 PM
Hi,
I'm having problems using a convertor on a WPF GridView
I'm trying to set the background colour.
The background sets correctly when I highlight any particular row in the GridView, however all other columns get set to this colour in the entire grid.

e.g. If I Click a row where the person has account balance 0 to 5 (e.g. red) every row goes red in this column
If I click a row where the person has account balance that converts to green every row goes green in this column and so on.

Below is the convertor, class person and XAML from my project.



CONVERTER:
Public Class AccountBalanceToColourConvertor
    Implements IValueConverter


    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim x As Double
        x = value
        Select Case x
            Case 0 To 5
                Return "Red"
            Case 5.01 To 50
                Return "Orange"
            Case Is > 50
                Return "Green"
        End Select
    End Function


    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        ' Only is a one way conversion
    End Function
End Class

CLASS PERSON
Public Class Person
    Public Property Forename As String
    Public Property AccountBalance As Double
    Public Enum Gender
        Male = 1
        Female = 2
        Unknown = 3
    End Enum
End Class



XAML

    <Window.Resources>
        <my1:AccountBalanceToColourConvertor x:Key="AccountBalanceToColourConvertor" />
        <CollectionViewSource x:Key="PersonViewSource" d:DesignSource="{d:DesignInstance my1:Person, CreateList=True}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource PersonViewSource}">
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ComboBox1" 
               
                  VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="147,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <telerik:RadGridView Height="136" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,62,0,0" Name="PersonRadGridView" VerticalAlignment="Top" Width="352" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn  DataMemberBinding="{Binding AccountBalance}"  Header="Account Balance" Width="Auto" DataFormatString="N2"
                                             Background="{Binding Path=AccountBalance, Converter={StaticResource AccountBalanceToColourConvertor}}">


            </telerik:GridViewDataColumn>
          </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>



1 Answer, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 02 May 2011, 02:28 PM
Hello Dermot,

In this very case, I would advise you to utilize our StyleSelector logic and implement a CellStyleSelector for this column. For more extensive information, you may look at this example. Please, do not hesitate to contact us immediately if you have any further inquiries.

All the best,
Ivan Ivanov
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
Dermot
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or