0

I'm new to wpf and I decided to make a simple To-Do app. I list the tasks in a listview, the number of tasks varies, so I created a list then defined the checkbox list as the item source of the listview. My problem is that I can't trigger an event when a checkbox is checked. I tried this but it's not efficient and the tasks don't disappear until I move my mouse:

private void lw_liste_MouseMove(object sender, MouseEventArgs e)
    {
        for (int i = 0; i < tasks.Count; i++)
        {
            if((bool)tasks.ElementAt(i).IsChecked)
            {
                tasks.RemoveAt(i);
                lw_liste.Items.Refresh();
            }
        }
    }

how can I trigger an event when a checkbox in a list is checked so I can remove them from the list?

Batur
  • 3
  • 2
  • 1
    Checkbox has command. https://stackoverflow.com/questions/5566050/executing-a-command-on-checkbox-checked-or-unchecked I would use relativesource to bind to the datacontext of the listview. Commandparameter for the row's datacontext. You can then use tye parameter to give your a reference to the item/row viewmodel from the observablecollection bound to the itemssource of your listview. – Andy Apr 27 '21 at 19:06

1 Answers1

0

There are a lot of possibilities. You could for example set a handler for check event, if you want to do it in code behind (I would prefer an MVVM):

<ListView.Resources>
    <Style TargetType="CheckBox">
        <EventSetter Event="Checked" Handler="CheckBox_Checked"></EventSetter>
    </Style>
</ListView.Resources>

Remember, there is also Unchecked event.

Rekshino
  • 6,954
  • 2
  • 19
  • 44