Binding Hex value to Color in XAML

2

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

c#
winrt-xaml
asked on Stack Overflow Mar 18, 2013 by Dan14021 • edited Mar 18, 2013 by Dan14021

3 Answers

3

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;
answered on Stack Overflow Mar 18, 2013 by Michaƫl Hompus
0

The problem seems to be resolved after recompiling.

answered on Stack Overflow Mar 21, 2013 by Dan14021
0

In your posted converter code, you are returning Color.Red, so no matter what value is, you'll get Red every time.

answered on Stack Overflow Feb 4, 2014 by Lance McCarthy

User contributions licensed under CC BY-SA 3.0