ListView.Items are always null as ListViewItem in a ListView - WPF

1.4k Views Asked by At

I have a listview which shows what drivers are not visible on the user interface. With the use of this listview it is possible to make them visible again. The functionality works perfect, but in order to give the end-user some clearness what drivers are selected I want to change the background-color of the listviewitem to yellow (for example). If they select this listviewitem again I want to change the background-color back to normal.

This is the code in my view:

        <ListView  Margin="10,10,10,10" x:Name="lstViewCollapsedDrivers" ItemsSource="{Binding CollapsedDrivers}" Grid.Row="1" Grid.Column="2"
                  ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="LstViewCollapsedDrivers_OnSelectionChanged">

            <i:Interaction.Triggers>
                <i:EventTrigger SourceName="lstViewCollapsedDrivers" EventName="MouseUp">
                    <i:InvokeCommandAction Command="{Binding SelectCollapsedDriverCommand}" CommandParameter="{Binding ElementName=lstViewCollapsedDrivers, Path=SelectedItem}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Height" Value="50"/>
                    <Style.Triggers>  
                        <DataTrigger Binding="{Binding Path=RowSelected}" Value="True">
                            <Setter Property="Background" Value="Yellow"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}">
                        <GridViewColumnHeader Style="{StaticResource gridViewColumnHeaderStyle}" Content="Name"/>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding="{Binding Haulier}">
                        <GridViewColumnHeader Style="{StaticResource gridViewColumnHeaderStyle}" Content="Haulier"/>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding="{Binding Plate}">
                        <GridViewColumnHeader Style="{StaticResource gridViewColumnHeaderStyle}" Content="Plate"/>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding="{Binding Telephone}">
                        <GridViewColumnHeader Style="{StaticResource gridViewColumnHeaderStyle}" Content="Telephone"/>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

The problem is, the ListView.Items are now of type Driver instead of ListViewItem (I think by this: ItemsSource="{Binding CollapsedDrivers}"). I tried many several things and there is no way I've found to parse the items back to ListViewItems.

I also added some code behind (actually I try to avoid this, but if there is no other way...).

  private void LstViewCollapsedDrivers_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var test = sender as ListView;
        var lstviewItem = test.SelectedItem ; // This is of type driver...
    }


  private void LstViewCollapsedDrivers_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var test = sender as ListView;
        var lstviewItem = test.SelectedItem as ListViewItem; // This is null...
    }

Anyone knows how to fix this? Thanks :)

1

There are 1 best solutions below

2
On BEST ANSWER

You can use the ItemContainerGenerator.ContainerFromItem method to generate the ListViewItem from your Driver object:

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListView listView = sender as ListView;
    Driver selectedDriver = listView.SelectedItem ; 
    ListViewItem item = (ListViewItem)
        listView.ItemContainerGenerator.ContainerFromItem(selectedDriver);
}