9

I am having a design problem and I know there has to be a way to make it work. I tried the solutions here: Annoying auto scroll of partially displayed items in WPF ListView But they didnt work for me because I am not allowed to work in the code-behind. I have a list of items from a wpf ListBox. like this:

Listbox picture

when I try to select the CheckBox in line 5, the Window centers on it but does not check it. After further testing, I found that it will not select the CheckBox as long as the bottom border of the item is not in view.

Here is the xaml for the ListBox and its Style:

<ListBox Grid.Column="0" Grid.Row="1" Name="RequestCheckoutV"
         ItemsSource="{Binding Path=CheckoutVM, Mode=TwoWay, IsAsync=True}"
         SelectedItem="{Binding Path=SelectedPermit}"
         BorderThickness="0"
         KeyboardNavigation.TabNavigation="Continue">
  <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
      <Setter Property="Background" Value="Transparent" />
      <Setter Property="Control.HorizontalContentAlignment" Value="Center"/>
      <Setter Property="Control.VerticalContentAlignment" Value="Top"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBoxItem}"  >
            <ContentPresenter />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

What can I do to make this select the checkbox instead of just centering it? Any help is appreciated.

7
  • If some other info is needed, please let me know. I will provide whatever is needed to get to the solution. Commented Aug 24, 2016 at 13:55
  • Giving the question a negative point without telling me why doesn't help me out. If something is wrong with the question please let me know so I can fix it. Thank you. Commented Aug 24, 2016 at 15:42
  • Add ClickMode="Press" property to the CheckBox for HitTestVisibility Commented Aug 29, 2016 at 15:43
  • it Worked Chris! add that as a solution so I can give you the Bounty Commented Aug 29, 2016 at 21:50
  • I know dude, I was trying to let you keep your points. :) Commented Aug 29, 2016 at 21:51

2 Answers 2

6
+50

So by adding the specification of "Press" to ClickMode property to CheckBox via ClickMode="Press" you're telling that object to basically ignore the event otherwise hijacked by the list item template. Now it will accept the first event in it's hit area for itself instead of the list item since it's default is "Release". Cheers :)

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

5 Comments

How about an easy way to stop it from centering? Right now its kind of disorienting the way the line item jumps to center...
@tCoe hmmm, if I remember right the last time I had to get rid of that I just did a quickie workaround by setting ScrollViewer.CanContentScroll="False" and disabling the ScrollViewer on the ListBox and embedding it in a separate ScrollViewer like <ScrollViewer><ListBox/></ScrollViewer> to just remove that functionality. Otherwise I don't recall any specific property to turn that "feature" off. I was in a rush at the time but if I remember right it worked fine as a workaround.
the ScrollViewer.CanContentScroll = "false" did the trick. didn't have to do the disabling part. Thanks Again Chris.
No sweat, pay it forward :)
Can you take a look at this? stackoverflow.com/questions/50219002/…
3

Try adding to your .ItemContainerStyle:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="IsSelected" Value="True"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

This will effectively give focus to anything you click in there.

1 Comment

the ItemContainerStyle is not found in ListView. I am using a ListBox. should I switch them all to a list view?

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.