Freddx L.
Freddx L.

Reputation: 635

Change ListViewItem background color by using an index specified by a property

I'm new to WPF, I have a property that defines an int which is the index of the element to change the background color within the ListView but is changing the background color of all the ListViewItem, here my code:

<ListView AlternationCount="2147483647" x:Name="listView1" HorizontalAlignment="Left" Height="295" Margin="10,10,0,0" VerticalAlignment="Top" Width="380" Padding="0" HorizontalContentAlignment="Center" BorderThickness="1" IsSynchronizedWithCurrentItem="False" SelectionMode="Single" ScrollViewer.CanContentScroll="True">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="{x:Type ListViewItem}">
                            <Setter Property="ContextMenu" Value="{StaticResource ListViewItemContexMenuKey}"/>

                            <Style.Triggers>
                                <DataTrigger Value="True">
                                    <DataTrigger.Binding>
                                        <MultiBinding Converter="{StaticResource IndexConverter}">
                                            <Binding Path="AlternationIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"/>
                                            <Binding Path="TargetIndex" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}"/>
                                        </MultiBinding>
                                    </DataTrigger.Binding>
                                    <Setter Property="Background" Value="#FFFF7171"/>
                                </DataTrigger>
                            </Style.Triggers>

                        </Style>
                    </ListView.ItemContainerStyle>
   </ListView>

Here the IndexConverter

public class IndexConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int index = (int)values[0];
        int targetIndex = (int)values[1];

        Console.WriteLine($"Index: {index}, TargetIndex: {targetIndex}");
        return index == targetIndex;
    }

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

In the IndexConverter.Convert the local variable index is the AlternationIndex and always is returning 0.

I don't get why this behaviour, when using trigger it works corretly, but you can't bind using Trigger.

<Trigger Property="ItemsControl.AlternationIndex" Value="1">
        <Setter Property="Background" Value="#FFFF7171"/>
</Trigger>

Upvotes: 0

Views: 663

Answers (1)

mm8
mm8

Reputation: 169200

As pointed out by @elgonzo, ItemsControl.AlternationIndex is an attached property that you should bind to like this:

<Binding Path="(ItemsControl.AlternationIndex)" 
         RelativeSource="{RelativeSource Self}"/>

Note the parentheses around the path.

Upvotes: 2

Related Questions