views:

167

answers:

3

Hi All

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.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsThreeState="False" 
                                                      Checked="OnChkChecked"
                                                      Unchecked="OnChkChecked"
                                                      IsChecked="{Binding IsCorrect}"></CheckBox>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
+3  A: 

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

http://stackoverflow.com/questions/2520391/how-to-get-checked-items-in-a-wpf-listbox

and

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

Veer
+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
+2  A: 

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 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
+1 Kajillion. Simple, works. Love it.
Riddari
A: 

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
Did you **try** @majocha's answer?
ANeves
...yeah, after I typed in my answer...D'oh....a much better solution.
ozczecho