Vallo
Vallo

Reputation: 1967

UWP Bind Listview items foreground to property

I want to change the foreground of certain items in a ListView based on a property of such items. If the item has the property "EsBlacklist" set to true, its foreground should be red.

<Page.Resources>
    <converter:ForegroundColorConverter x:Key="ForegroundConverter" x:Name="ForegroundConverter"/>
</Page.Resources>

<StackPanel Grid.Column="1" Grid.Row="1">
            <TextBlock HorizontalAlignment="Center" Margin="10" FontSize="24">Vehículos sin VTV</TextBlock>
            <ListView ItemsSource="{x:Bind ViewModel.PatentesSinVtv}" Margin="10" DisplayMemberPath="Placa" 
                      SelectedItem="{x:Bind ViewModel.PatenteSeleccionada, Mode=TwoWay}"
            HorizontalAlignment="Center"
            IsItemClickEnabled="False"
            IsSwipeEnabled="False"
            CanDragItems="False"
            SelectionMode="Single"
            Grid.Column="1"
            Grid.Row="1">
                <ListViewItem Foreground="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ForegroundConverter}}"></ListViewItem> 
( Self should reference the item and not ListViewItem.)
            </ListView>    
</StackPanel>

And the converter:

class ForegroundColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var patente = (Patente)value; //value is not a Patente but ListViewItem
            return patente.EsBlacklist ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Gray);
        }
    }

My problem is that "value" received in the converter is not a Patente but a ListViewItem

Upvotes: 0

Views: 230

Answers (4)

Vallo
Vallo

Reputation: 1967

I needed to implement an ItemTemplate

<ListView.ItemTemplate>
    <DataTemplate x:DataType="modelo:Patente">
        <TextBlock Text="{Binding Placa}" Foreground="{x:Bind EsBlacklist, Mode=TwoWay, Converter={StaticResource ForegroundConverter}}"></TextBlock>
    </DataTemplate>
</ListView.ItemTemplate>

and the converter becomes this:

public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (bool)value? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Gray);
    }

Upvotes: 0

Breeze Liu - MSFT
Breeze Liu - MSFT

Reputation: 3808

My problem is that "value" received in the converter is not a Patente but a ListViewItem

As the document of {RelativeSource} markup extension,

{RelativeSource Self} Produces a Mode value of Self. The target element should be used as the source for this binding. This is useful for binding one property of an element to another property on the same element. ...

The Self mode is useful for binding one property of an element to another property on the same element, and is a variation on ElementName binding but does not require naming and then self-referencing the element.

Here is an example to use the RelativeSource={RelativeSource Self},

<Rectangle
  Fill="Orange" Width="200"
  Height="{Binding RelativeSource={RelativeSource Self}, Path=Width}"/>

You can see the document to learn how to use {RelativeSource} markup extension

You can bind the ViewModel directly to make the converter be the Patente,

<ListViewItem Foreground="{x:Bind ViewModel, Mode=TwoWay, Converter={StaticResource ForegroundConverter}}"/>

Upvotes: 1

Jet  Chopper
Jet Chopper

Reputation: 1488

You may deal with it like this:

<ListViewItem>
    <ListViewItem.Foreground>
        <SolidColorBrush Color="{x:Bind YourColor}"/>
    </ListViewItem.Foreground>
</ListViewItem>

Upvotes: 0

Maxim Zabolotskikh
Maxim Zabolotskikh

Reputation: 3367

The value of foreground color is not a plain color, but a brush. So your converter should return new SolidColorBrush(Colors.Red).

Upvotes: 0

Related Questions