1

I have a ListView with a CheckBox inside of the ListView's DataTemple. I was shown how to make the Command work. I would like to capture the ListView SelectedItem to pass as a parameter to the Command, but I don't have it right...

<ListView x:Name="lvReferralSource" ItemsSource="{Binding ReferralObsCollection}" Style="{StaticResource TypeListViewStyle}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="200">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>

                                        <CheckBox x:Name="ckbReferralIsChecked" Content="{Binding Value}" IsChecked="{Binding Active}" Style="{StaticResource CheckBoxStyleBase2}"
                                                  Command="{Binding DataContext.CheckBoxIsChecked, RelativeSource={RelativeSource AncestorType=ListView}}" 
                                                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=SelectedItem}">
                                        </CheckBox>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
mjordan
  • 319
  • 2
  • 22
  • In your case the selected item is the check box item, isn't it, because only check box present as the list views item, if you do this it will pass the content of the check box as the parameter `CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=Content}"` – TheLastStark Jun 04 '19 at 05:06
  • @mjordan: Your current binding should work provided that there is an item selected. But why don't you simply bind to the *current* item regardless of whether it's actually selected? `CommandParameter="{Binding}"`. – mm8 Jun 04 '19 at 13:24
  • That's what I was thinking as well, but I assumed since he has 2 columns, he was planning on adding something else later in there as well, something like, if selected from the list the check boxes become enabled – TheLastStark Jun 04 '19 at 13:40

1 Answers1

3

Looking at the problem again I think I understood it correctly now. Here is a different approach to get the SelectedItem from the ListView Then in the CheckBox I bound the CommandParameter as below

CommandParameter="{Binding ElementName=lvReferralSource, Path=SelectedItem}"

The following will pass the object related to the CheckBox

CommandParameter="{Binding}"// Full object from the ListView

In the Command Method related to the CheckBox you can cast the parameter object to the correct type(type of the objects in the ListView ItemSource) and get the value of Value and Active

TheLastStark
  • 770
  • 5
  • 18
  • Thanks. That looks like it should work, but "null" is passed to the parameter. I'm looking trying to get both the "Value" and "Active" properties from the checkbox. – mjordan Jun 04 '19 at 11:01
  • It passes the relevant selected object from the ItemSource of the list, if it is null then either you haven't selected an item yet or objects in ItemSource is null – TheLastStark Jun 04 '19 at 11:18
  • You can access the Value from the SelectedItem right? – TheLastStark Jun 04 '19 at 11:20
  • Thanks! The second option worked perfectly. Passed the object. – mjordan Jun 04 '19 at 23:22
  • This is the solution I've been looking for to remove an item from a CollectionView when it's deleted from a SwipeView! – Martin Jan 03 '23 at 03:36