0

So I'm trying to bind the background of a control. Everything works fine, but with the latest specifications of the project I saw, that i need to change the background color dependent on two values. The second value should be the content of a label, which indicates which color to use.
Currently the used converter is of the cell Battery (myConverter)
The element to bind is the background color of a cell in a Gridview. The xaml code looks like this:

<DataGrid 
    Background="Transparent" 
    ItemsSource="{Binding Source={StaticResource Properties}, Path=TableData}"
    AutoGenerateColumns="False" 
    IsReadOnly="True"
    Name="Datatable"
    VerticalScrollBarVisibility="Visible"
    BorderThickness="0"
    >
    <DataGrid.Resources>
        <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        </Style>
        <Style TargetType="ScrollBar">
            <Setter Property="Width" Value="15" />
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="Background" Value="Transparent"></Setter>

        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns >
        <DataGridTextColumn Width="90" Header="ID" Binding="{Binding ID}"
                            >
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground" Value="{Binding ID, Converter={StaticResource myIDConverter}}"></Setter>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Width="90" Header="Batterie" Binding="{Binding Battery}" >
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Width="90" Header="Current Use" Binding="{Binding CurrentUse}"></DataGridTextColumn>
        <DataGridTextColumn Width="90" Header="Occupancy" Binding="{Binding Occupancy}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Background" Value="{Binding Occupancy, Converter={StaticResource myConverter}}"></Setter>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

The code of the converter looks like this:

public class BatteryConverter : IValueConverter, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush clr;
        if (int.Parse(value.ToString()) >= 80)
            clr = new SolidColorBrush(Colors.Green); 

        else if (int.Parse(value.ToString()) >= 40)
            clr = new SolidColorBrush(Colors.Orange);
        else if (int.Parse(value.ToString()) >= 0)
            clr = new SolidColorBrush(Colors.Red);
        else
            clr = new SolidColorBrush(Colors.White);

        return clr;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
  • 2
    Soooo... What’s not working? – blins May 20 '18 at 14:17
  • If it is just that you need to compare other state from the value converter in order to determine color then maybe consider also using `CoverterParameter` in order to share state. See this similar question: https://stackoverflow.com/questions/5702737/how-to-pass-specific-value-to-the-converter-parameter – blins May 20 '18 at 14:26
  • "The second value should be the content of a label" -- what label? Where? That doesn't seem like a good idea anyway: Labels do not generate their own content. Their content comes from *something else*. Instead of binding to a Label's content, bind to the *source* of the label's content. Are you able to find out where the label's content comes from? – 15ee8f99-57ff-4f92-890c-b56153 May 21 '18 at 14:08

1 Answers1

0

i recommend using MVVM-Pattern, that makes everything easier and cleaner in WPF :) and this case would be super easy but anyway.

im actually not at my Dev.Pc so i can't give u the final source, but in your case you are good to go with style triggers like => MultiTrigger/MultidataTrigger ....

If the label content is available as property, you should use MultiDataTrigger(s) then you don't need to use a converter. -> you can simply use multipleDataTriggers for each result.

Hope this answer helped you. It should be easy to google how to implement this style triggers. If not let me know and i'll give you some sample code when im at my dev pc

greetz

Priemar
  • 96
  • 5