user2955610
user2955610

Reputation: 835

WPF Set Label Background according to its Value using DataBinding

I am currently making a grid that in each cell there is a label, the content in the labels are coming from a list using DataBinding. I am trying to change the color of each cell depending on the value in the label. Eg if value = 1 then the background must be black. This is the code i have now:

  <Window.Resources>
    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding}"  Width="70" Height="70" HorizontalContentAlignment="Center">
        </Label>
    </DataTemplate>

    <DataTemplate x:Key="DataTemplate_Level1">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</Window.Resources>

I tried different ways using triggers but nothing seemed to Work.

Any help would be appreciated.

Thanks

Upvotes: 4

Views: 1836

Answers (1)

It's as easy as this, but you'll be copying and pasting a lot of Setter tags. You might want to consider a value converter instead (see below).

<DataTemplate x:Key="DataTemplate_Level2">
    <Grid 
        SnapsToDevicePixels="True" 
        x:Name="Background">
        <Label Content="{Binding}" />
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="1">
            <Setter TargetName="Background" Property="Background" Value="Black" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="2">
            <Setter TargetName="Background" Property="Background" Value="Khaki" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="3">
            <Setter TargetName="Background" Property="Background" Value="YellowGreen" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

And here's the value converter version:

<Window.Resources>
    <local:ColorConverter x:Key="ColorConverter" />

    <DataTemplate x:Key="DataTemplate_Level2">
        <Grid 
            SnapsToDevicePixels="True" 
            Background="{Binding Converter={StaticResource ColorConverter}}">
            <Label Content="{Binding}" />
        </Grid>
    </DataTemplate>
</Window.Resources>

C#

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Color clr = Colors.SteelBlue;

        var s = value as String;

        //  Add code here to pick a color or generate RGB values for one
        switch (s) {
            case "1":
                clr = Colors.Black;
                break;
        }

        return new SolidColorBrush(clr); 
    }

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

Upvotes: 4

Related Questions