13

I have a Listview that has a checkbox as one of the columns. If I click anywhere but the actual checkbox the SelectedItem of the ListView is set to the current selected row, as expected. If, on the other hand I click onto the checkbox (without clicking on the row first) then the SelectedItem is null or the previously clicked row.

Can anyone help me out....

Cheers

<ListView Width="auto" SelectionMode="Single" x:Name="listBox"  ItemsSource="{Binding MyData}" SelectedItem="{Binding Path=SelectedMyData}">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Date" Width="120">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <ContentPresenter Style="{StaticResource DateTimeContent}" Content="{Binding MyDate}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Is Correct" Width="100">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsThreeState="False" 
                                                      Checked="OnChkChecked"
                                                      Unchecked="OnChkChecked"
                                                      IsChecked="{Binding IsCorrect}"></CheckBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>




                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsThreeState="False" 
                                                      Checked="OnChkChecked"
                                                      Unchecked="OnChkChecked"
                                                      IsChecked="{Binding IsCorrect}"></CheckBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
Peter
  • 9,643
  • 6
  • 61
  • 108
ozczecho
  • 8,649
  • 8
  • 36
  • 42

3 Answers3

17

It's very easy, just handle Click event on your checkbox:

private void CheckBox_Click(object sender, RoutedEventArgs e) {
    var cb = sender as CheckBox;
    var item = cb.DataContext;
    myListView.SelectedItem = item;
}
majocha
  • 1,161
  • 6
  • 12
  • +1 Very easy indeed. I should get some glasses. I read your answer and I thought you misunderstood the question. It was me who mis-understood your answer. Very simple.... Thanks :-) – ozczecho Apr 12 '10 at 12:53
  • I have been searching for hours and this is exactly what I was looking for I think! It works perfectly. Thank you so much! So simple indeed. – nitefrog Feb 09 '11 at 08:49
3

You have to parse your visual tree to get the index of the checkbox that is checked and select that particular listbox item in your code whenever some checkbox is checked

You may also be interested in

How to get checked items in a WPF ListBox?

and

http://goalbook.wordpress.com/2009/09/05/wpf-checkedlist-control/

Community
  • 1
  • 1
Amsakanna
  • 12,254
  • 8
  • 46
  • 58
  • +1 .... sorry for late reply. Yes, had to parse the visual tree, but not in the as suggested by your links. Nonetheless, thanks for the reply. See my answer. – ozczecho Apr 12 '10 at 12:32
0

Veer suggested parsing the visual tree to get the checkbox. The things is I already had the checkbox. What I needed was the listviewitem that held the checkbox. After further research this blog post pointed me in the right direction. Here is the code to get the listviewitem of the row that the checkbox was clicked:

        private void chkbox_Checked(object sender, RoutedEventArgs e)
    {
        DependencyObject dep = e.OriginalSource as DependencyObject;
        while ((dep != null) && !(dep is ListViewItem))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep != null)
        {
            IMyViewModel vm = DataContext as IMyViewModel;
            vm.SelectedThing = (MyListItemViewModel)lst.ItemContainerGenerator.ItemFromContainer(dep);
            vm.DoSomethingCommand.Execute(e.RoutedEvent.Name.ToLower());
        }
    }
ozczecho
  • 8,649
  • 8
  • 36
  • 42