0

in my WPF program, I have a ListView with a column with a checkbox:

    <ListView>
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Checkbox-Column">            
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <Border BorderThickness="2" BorderBrush="AntiqueWhite">
                  <CheckBox IsChecked="{Binding Status}" />
                </Border>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
        </GridView>
      </ListView.View>
   </ListView>

When the user clicks on a checkbox, it should change it's state immediately. But actually, the user needs two clicks. The first click selects the row (or puts it into "edit-mode", though I'm not sure about this), and the second click then changes the checkbox state.

What can I do to force the checkbox to change with the first click?

EDIT: The reason for this behaviour is, that I use a bool? instead of a bool to bind against the checkbox. The initial value of the property is null (and it has to be), so the first click changes the value to false and the second to true (which is why it seems that the control doesn't react to the first click).
So - the question should be: How can I force the control to change from null to true and not to false?

Thanks in advance,
Frank

2
  • 2
    I can't reproduce your issue. Which control is focused when you click the first time? Commented Oct 23, 2017 at 14:41
  • I played around a bit with it and realized, that the reason for the behaviour is the use of a bool? instead of a bool to bind against the checkbox (see my edited question) Commented Oct 24, 2017 at 10:49

2 Answers 2

1

How can I force the control to change from null to true and not to false?

Write some code in the view that sets the IsChecked property to true the first time it is set to false:

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    var cb = e.Source as CheckBox;
    cb.IsChecked = true;
    cb.Unchecked -= CheckBox_Unchecked;
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    var cb = e.Source as CheckBox;
    cb.IsThreeState = false;
}

<CheckBox IsChecked="{Binding Status}" IsThreeState="True"
          Checked="CheckBox_Checked"
          Unchecked="CheckBox_Unchecked"/>
Sign up to request clarification or add additional context in comments.

4 Comments

This almost does it! If I don't set the IsThreeState to true (I don't need the third state for the user, as the meaning of null in my context is, that the user didn't interact with the row), repeated clicking on a checkbox leads to the following behaviour: null (initial value) » true » true » false » true » false » true ... So, there is the second click that should change the checkbox to false but it remains true. After that, it switches as expected. Any idea what might cause this?
Set the IsThreeState property to true in the XAML and set it back to false in the event handler.
Looks like the State changes from Null to true, when IsThreeState is true. So the solution is to subscribe to the Checked event and set IsThreeState to false in the handler. Thanks a lot for helping me figuring that out! :)
I gladly accept your answer when you edit it towards our findings, to reflect the solution without having to go through the comments.
0

You have to set VerticalAlignment and HorizontalAlignment of your CheckBox (and possibly your Border) to Stretch.

This is done by default on most controls, but there is a possibility that you either have it overriden by explicit styling or your ListView overrides it for its children by default (not sure how it behaves).

2 Comments

And how should that influence the behaviour of needing two clicks to select/deselect the checkbox?
It shouldn't work like this. You must have some other styling defined somewhere else which overrides the default behaviour of your controls. You can try to check it by creating an empty style with a key an assigning it to your control. It will then override all other styling done in your application for this control (you may see your styles set to be applied to a given type of control, that's why you may not see it assigned explicitly, while it still works).

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.