C# – Determining Which ListViewItem Checkbox existed in


I saw something about this, but I've lost it now, and can't find it again. What I need is to know which ListViewItem a checkbox exists in, when the checkbox is checked (Which will eventually lead to its deletion),

I tried getting the parent, but apparently that doesn't work.


<ListView Name="incomingMessages" Extensions:ListViewColumns.Stretch="true"
  Height="226" Canvas.Left="0" Canvas.Top="0" Width="755" ItemsSource="{Binding Messages}">
      <GridViewColumn Header="Time" Width="100" DisplayMemberBinding="{Binding Time}" />
      <GridViewColumn Header="Message" DisplayMemberBinding="{Binding FullMessage}"  />
      <GridViewColumn Header="Phone Number" DisplayMemberBinding="{Binding Number}" Width="85"  />
      <GridViewColumn Header="Done" Width="45">
            <CheckBox IsChecked="{Binding Done}" VerticalAlignment="Center" />

What sort of code needs to go in the Checked event to make it work? (As a side question, why does my VerticalAlignment not center align my checkboxes in the column?)

Like I said I tried parent in this sort of code

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
            CheckBox cb = (CheckBox)sender;
            object lv = cb.parent;

And if I breakpoint on the cb.parent line, cb.parent is null.


Best Solution

Ok, so what I've worked out is to do this

<CheckBox IsChecked="{Binding Done}"
    Binding Path=Items.Count,
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}
  HorizontalAlignment="Center" />

Which is working (aside from the first three which are added, because they are added at runtime and seem to bypass it, however that won't happen in live version so thats ok. But then I can see this leading to problems when deleted, as the Uid will be maintained, yet the DataSource will not match up. So ideas?