KarkMump
KarkMump

Reputation: 81

Selecting a listview row with a checkbox

I'm calling an api into a WPF listView. I added a CheckBox to each row and am trying to select a row the CheckBox and then display that value.

My xaml file:

            <ListView x:Name="myListView"  
                      SelectedValue="{Binding title}" 
                      Height="550" Margin="35,149,-202.2,0" 
                      VerticalAlignment="Top"
                      Background="AntiqueWhite" Grid.ColumnSpan="2"                   
                      SelectionChanged="MyListView_SelectionChanged">
                <ListView.ItemTemplate>                
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <CheckBox 
                                Name="myCheckBox"
                                Click="listView_Click"
                                IsChecked="{Binding IsChecked}" 
                                Margin="5, 0"/>
                            <TextBlock Text=" " />
                            <TextBlock Text="{Binding title}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding publisher}" />
                            <TextBlock Text=", " />
                            <TextBlock Text="{Binding price}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>            
            </ListView> 

And my listView_Click method, with what I've tried and the output it produced

    private void listView_Click(object sender, RoutedEventArgs e)
    {
        //Selected_label.Text = myListView.ItemsSource.ToString();
        //Output = System.Collections.Generic.List`1[ShortBoxedUI.ShortBoxed+Comics]

        //Selected_label.Text = myListView.Items.ToString();
        //Output = System.Windows.Controls.ItemCollection

        //Selected_label.Text = myListView.ToString();
        //Output = System.Windows.Controls.ListView Items.Count:352                
     } 

I checked the past questions/answers and didn't find anything that aligned with what I'm trying to do.

Upvotes: 0

Views: 684

Answers (1)

Keith Stein
Keith Stein

Reputation: 6724

By default, the DataTemplate in ListView.ItemTemplate will bind to the data item from ItemsSource. If you want to bind to the ListViewItem itself (which is housing the DataTemplate), you can use RelativeSource:

...
IsChecked="{Binding IsSelected,
            RelativeSource={RelativeSource AncestorType=ListViewItem}}"
...

The correct property to get the currently selected item is SelectedItem, while SelectedValue would only be used in conjunction with SelectedValuePath.

And the event for doing something when the selected item changes is SelectionChanged.

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MyDataObject selectedItem = myListView.SelectedItem;
    Selected_label.Text = selectedItem.SomeTextProperty;
}

Commonly, though, you would bind to SelectedItem rather than handle SelectionChanged directly.

Upvotes: 1

Related Questions