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>
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>