0

My CheckBox Command does not work. I want to pass the SelectedItem of the ListView to the Command when the SelectedItem is checked or unchecked, but the Command does not execute at all. I also suspect my CommandParameter is not configured correctly?

I am pretty sure the problem is because the CheckBox is within a ListView DataTemplate.

Can someone show me how to set this up? I tried to follow examples I found, but nothing seems to work. thanks.

XAML

<ListView x:Name="lvReferralSource" ItemsSource="{Binding ReferralSourceTypeObsCollection}" Style="{StaticResource TypeListViewStyle}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Vertical">
      <CheckBox x:Name="ckbReferralIsChecked" Content="{Binding Value}" IsChecked="{Binding Active}" Style="{StaticResource CheckBoxStyleBase2}"
       Command="{Binding CheckBoxIsChecked}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=SelectedItem}">
      </CheckBox>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

CODE

private ICommand _CheckBoxIsChecked;
public ICommand CheckBoxIsChecked
{
    get
    {
        if (_CheckBoxIsChecked == null)
        {
            _CheckBoxIsChecked = new RelayCommand<object>(ExecuteCheckBoxIsChecked, CanExecuteCheckBoxIsChecked);
        }

        return _CheckBoxIsChecked;
    }
}
public bool CanExecuteCheckBoxIsChecked(object parameter)
{
    return true;
}
public void ExecuteCheckBoxIsChecked(object parameter)
{
    Mouse.OverrideCursor = Cursors.Wait;

    if (parameter != null)
    {
        //Do Stuff...
    }

    Mouse.OverrideCursor = Cursors.Hand;
}
2
  • Can you share the TypeListViewStyle and CheckBoxStyleBase2 code Commented Jun 3, 2019 at 8:21
  • Please also check output console for binding related error information. Commented Jun 3, 2019 at 9:54

1 Answer 1

1

Your command should get executed provided that the CheckBoxIsChecked property belongs to the data object where the Value and Active properties are defined.

If it belongs to the view model, you could bind to it using a RelativeSource:

<CheckBox x:Name="ckbReferralIsChecked" Content="{Binding Value}" IsChecked="{Binding Active}"
            Style="{StaticResource CheckBoxStyleBase2}"
            Command="{Binding DataContext.CheckBoxIsChecked, RelativeSource={RelativeSource AncestorType=ListView}}" 
            CommandParameter="{Binding}">
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome. Thanks. CheckBoxIsChecked is in the view model whereas Value and Active were in the model.
I posted a follow up question about the correct way to add the CommandParameter. I took a guess, but it's not quite right... stackoverflow.com/questions/56435313/…

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.