0

I'm populating a listview with checkbox listview items. Currently the only way to check the box is by clicking on just the box. How can I extend this to make the content of the listview item activate the checkbox also

           <CheckBox VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="2"
                  IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Value}" ContentTemplate="{Binding FilterValueTemplate, ElementName=Q_ROOT}"/>

Above was in my theming, below is the actual view.

            <StackPanel Orientation="Horizontal">                    
                <CheckBox VerticalAlignment="Center" Margin="2" IsChecked="{Binding IsChecked, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListViewItem}}">
                    <CheckBox.Content>
                        <ContentPresenter VerticalAlignment="Center" Content="{Binding Value}" ContentTemplate="{Binding FilterValueTemplate, ElementName=QFSP_ROOT}"/>
                    </CheckBox.Content>
                </CheckBox>
            </StackPanel>
3
  • You could add a SelectedItem binding in the viewmodel, and a mousedown event that will change the selected item checkbox IsChecked. sounds right? Commented Jul 30, 2014 at 20:14
  • i could, but that seems like a hack i shouldnt have to do. There has to be a way here in the xaml to bind it. Commented Jul 30, 2014 at 21:28
  • Well, if you are trying to bind a property like IsSelected, than I guess xaml is all that you need. If you want to bind a mouse gesture and rather do it mvvm -no code behind you could route the event to the viewmodel. Commented Jul 31, 2014 at 5:47

2 Answers 2

1

Bind IsChecked with IsSelected property of ListViewItem using RelativeSource :

<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource 
                              Mode=FindAncestor, AncestorType=ListViewItem}}"/>
Sign up to request clarification or add additional context in comments.

5 Comments

is saying mode and ancestortype arent found in type relative source
Doing this doesnt seem to change anything. Still cant click on the content.
@Mertis - I am not sure how you have tried it because it works when CheckBox is defined within ItemTemplate of ListView. Can you add more code to your question where you are defining this checkBox?
I changed the actual view to have what you show, the checking part of the box works now, but the uncheck doesnt.
My 2nd block of code i have in my question is what is now allowing me to check and uncheck the box, but now what this is tied to (a table filter) is no longer working.
0

I moved the content presenter content inside the checkbox itself and it now works fine:

<CheckBox VerticalAlignment="Center" Margin="2" IsChecked="{Binding IsSelected, Mode=TwoWay}" 
     Content="{Binding Value}" ContentTemplate="{Binding FilterValueTemplate, ElementName=QFSP_ROOT}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.