Cleve
Cleve

Reputation: 1433

How can I change background of a specific ListViewItem whose Index matches a changing value in the ViewModel?

I have a ListView whose ItemsSource is set to a collection property of my ViewModel. There is also a property on the ViewModel, say 'CurrentIndex', that changes as the code runs. As this changes I want ONLY the ListViewItem whose index in the ListView that matches 'CurrentIndex' to change, say its Background to change colour. As 'CurrentIndex' changes I want the previously referenced ListViewItem to be 'reset', i.e. its Background changes to it prior colour. Does anyone know how might I achieve this?

I thought of exploiting the SelectedIndex Property of the ListView but this doesn't work as the user can click on the ListView and change the selection thereby changing the background of the wrong item.

The ListViewItems are Templated via ItemTemplate in XAML.

Hope this makes sense.

Many thanks in advance for any help.

Upvotes: 0

Views: 695

Answers (1)

Lupu Silviu
Lupu Silviu

Reputation: 1165

Second solution:

If you have a array of Item that is bound to the ListView itemsSource, and each Item has INotifyPropertyChanged implemented, when you could do this:

<ListView ItemsSource="{Binding MyItemsList}">
        <ListView.ItemTemplate>
            <ItemContainerTemplate>
                <Grid>
                    <Grid.Style>
                        <Style TargetType="Grid">
                            <Setter Property="Background" Value="Transparent"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ShouldChangeBackground}" Value="true">
                                    <Setter Property="Background" Value="Red"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Grid.Style>

                </Grid>
            </ItemContainerTemplate>
        </ListView.ItemTemplate>
    </ListView>

ShouldChangeBackground is a Boolean inside Item. And you set this Boolean to true, when you want it to changed background color, and then set it to False, when it should be back to normal.

Another solution could be to use a Converter:

    class TrueToBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Boolean && (Boolean)value)
            return Brushes.Red;

        return Brushes.Transparent;
    }

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

and in your code:

 <ListView ItemsSource="{Binding Element, FallbackValue=123123213213123213213}">
        <ListView.ItemTemplate>
            <ItemContainerTemplate>
                <Grid Background="{Binding ShouldChangeBackground, Converter={StaticResource TrueToBackgroundConverter}}">
                </Grid>
            </ItemContainerTemplate>
        </ListView.ItemTemplate>
    </ListView>

Upvotes: 1

Related Questions