5

Problem:

I have a Listbox, in that Listbox are Checkboxes. On the first click the Checkbox is selected and checked. On the second click the checkbox only gets set. One can reselect a different Checkbox using the arrow keys. My goal is, that the checkbox gets selected first and then checked afterwards (clicking again on it) thus removing the need for Arrow keys.

Goal:

  • Select the Item on first click
  • Check the Checkbox on second click

The Code:

<ListBox Name="Terminals" ItemsSource="{Binding AllTerminals, Mode=OneWay}" IsSynchronizedWithCurrentItem="True">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Description}" Foreground="{Binding DisplayColor}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

1 Answer 1

7

disable click registration on CheckBox via binding IsHitTestVisible property to selection state of ListBoxItem:

<ListBox Name="Terminals" ItemsSource="{Binding AllTerminals, Mode=OneWay}" IsSynchronizedWithCurrentItem="True">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Description}" 
                      IsHitTestVisible="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                      Foreground="{Binding DisplayColor}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

this way the first click will only select ListBoxItem and checkboxes can be checked/unchecked by second click


after items are added to ListBox, the visual tree is the following:

ListBox
 --ListBoxItem
   --CheckBox
 --ListBoxItem
   --CheckBox
 --ListBoxItem
   --CheckBox

When user click on ListBoxItem, it gets selected (IsSelected=true). When user click on CheckBox, it gets checked or unchecked. But if IsHitTestVisible set to false, click on element will not be registered. Since check/uncheck should work only for selected items, we can create binding between CheckBox.IsHitTestVisible and parent ListBoxItem.IsSelected to achieve such effect

Sign up to request clarification or add additional context in comments.

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.