2

I have a ListView that contains my ObservableCollection object as an ItemsSource. This source can send random values. I want to change text block containing values to a specific color depending on the incoming values.

So if incoming values are in the range, text block should stay the same color, and if the value is out of a range, it should become Red.

I've tried with using OnPropetryChange, but I got nothing...

I also tried to bind to Foreground, but that did not work as well.

<ListView x:Name="ListView" ItemsSource="{Binding ReactorCollection}" HorizontalAlignment="Left" Height="310" Margin="348,10,0,0" VerticalAlignment="Top" Width="254" Grid.RowSpan="2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="1">
                        <Grid ShowGridLines="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition  Width="120"/>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="110"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Text="{Binding Id, Mode=TwoWay}"  TextAlignment="Center" FontSize="15" VerticalAlignment="Center"/>
                            <TextBlock  Grid.Column="1" Text="{Binding Name, Mode=TwoWay}" TextAlignment="Center" FontSize="15" VerticalAlignment="Center"/>
                            <TextBlock  Grid.Column="2" Text="{Binding Value, Mode=TwoWay}" TextAlignment="Center" FontSize="15" VerticalAlignment="Center"/>

                        </Grid>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

This is my XAML, of the ListView in items that it contains, so it's displaying Name, Id, and the Value.

If someone has a bit of advice on how I should implement this, it would help me a lot!

John Doe
  • 165
  • 1
  • 4
  • 15
Stefan
  • 375
  • 1
  • 9
  • 24

1 Answers1

1

You don't show any code that actually sets the colour.

You would probably do something like this to control the colour from a property that represents the value is out of range:

<TextBlock Text="{Binding Value}">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding OutOfRange}" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

In this example, there is an OutOfRange property on the item (the items in your ReactorCollection).

You could bind the Foreground property directly, but you need to use a ValueConverter (search for that if you'd like to go down that rabbit hole...).

This also assumes you have correctly bound your ListView, that the ReactorCollection property is being updated properly (hope you are adding items to it, not just overwriting it on incoming data) and that the class of item in the collection correctly implements INotifyPropertyChanged (if the values ever change after being added to the collection).

andrew
  • 1,723
  • 2
  • 12
  • 24
  • 1
    If you do want to go down the scary rabbit hole of ValueConverters, I think [WpfTutorials](https://www.wpftutorial.net/ValueConverters.html) is short, effective, and newbie friendly. – John Doe Dec 24 '19 at 04:26
  • 1
    The tip of "hope you are adding items to it, not just overwriting it on incoming data" bears repeating. Your code should only ever call `ReactorCollection = something` *once*, when initializing, and *never* overwrite that by calling `ReactorCollection = anything else`. Doing so will break databindings. – John Doe Dec 24 '19 at 04:30