Background color not being set with binding and converter in UWP app

1.9k Views Asked by At

I try to set the background color of a listitem (or for testing purposes the text foreground color of a TextBlock) in a UWP app using binding to a property of the binded item (TimeEntry).

This is the Xaml for the ListView which is bound to a collection of TimeEntries (relevant TextBlock on second last line):

...
    <Page.Resources>
        <local:TimeEntryTypeColorConverter x:Key="TimeEntryTypeColorConverter" />
    </Page.Resources>
...
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1">
            <TextBlock Text="Xy:" />
            <ListView>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel></StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                ....
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Test" Grid.Row="0" Grid.Column="0" Foreground="{Binding Type, Converter={StaticResource TimeEntryTypeColorConverter}}" />
                            ...

The TimeEntry class has an enum of 'TimeEntryType' and a 'Type' property:

public enum TimeEntryType
    {
        Unknown,
        Standard,
        Break
    }

public TimeEntryType Type
{
    get
    {
        if (_isBreak)
        {
            return TimeEntryType.Break;
        }
        return TimeEntryType.Standard;
    }
}

And this is what the converter for this property/enum looks like:

public class TimeEntryTypeColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return null;
        var timeEntryType = (TimeEntry.TimeEntryType)value;
        if (timeEntryType == TimeEntry.TimeEntryType.Break)
            return Colors.LightGray;

        return Colors.Transparent;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

I have other bindings to the TimeEntry object of the ListView item collection that work. And this binding seems also to work, as the debugger shows me that the converter is being used and it would also convert to "LightGray" for example for 'Break'. But there is no change in the UI, other bindings get directly updated, so the binding normally works.

I don't understand why the UI doesn't get updated to 'LightGray' althought it seems like the converter is being correctly used and returns this value as Foreground or Background color.

1

There are 1 best solutions below

0
On BEST ANSWER

As the documentation shows; Foreground expects a Brush and not a Color.

You can fix your issue with:

var color = // Select your color here //
var brush = new SolidColorBrush(color);

Essentially, a color is fairly self-explanatory, but a brush is an actual "material" to paint something with.