I have objects stored in a database that I am displaying in a GridView. I am binding each of their properties from the database. The color property is stored as a Hex value.
I am trying to bind this hex value using a converter function as shown below and just returning Red every time for now.
It seems to be working but it eventually returns the following error: The program '[5548] TranslatorService.Example.exe: Managed (v4.0.30319)' has exited with code -1073741189 (0xc000027b).
Can anyone tell me what I am doing wrong?
The code-behind:
public class StringToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, String language)
{
return Colors.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, String language)
{
throw new NotImplementedException();
}
}
The XAML:
<Grid.Background>
<SolidColorBrush Color="{Binding Path=ColorHex, Converter={StaticResource ColorConverter}}" />
</Grid.Background>
Thank you
In my experience you need to assing a Brush, not a Color:
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 0, 0);
or
mySolidColorBrush.Color = Color.Red;
The problem seems to be resolved after recompiling.
In your posted converter code, you are returning Color.Red, so no matter what value
is, you'll get Red every time.
User contributions licensed under CC BY-SA 3.0