tags:

views:

307

answers:

2

Hey Guys,

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.

XAML:

<ListView Name="incomingMessages" Extensions:ListViewColumns.Stretch="true"
  Height="226" Canvas.Left="0" Canvas.Top="0" Width="755" ItemsSource="{Binding Messages}">
  <ListView.View>
    <GridView>
      <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">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <CheckBox IsChecked="{Binding Done}" VerticalAlignment="Center" />
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

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.

Thanks, Psy

A: 

You can use VisualTreeHelper to walk up the visual tree and find the ListViewItem. However, I'd recommend steering clear of this approach if possible. It is most likely possible to achieve your goal using other WPF features like data binding.

HTH,
Kent

Kent Boogaart
Thats what I was thinking, my current through process is binding the Uid to the index of the listviewitem, not sure how to do that though
Psytronic
I thought it might be this{Binding Path=Index, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}However it's throwing "AncestorType must be specified for FindAncestor".
Psytronic
Ok, sort of got that working, but can't find the right attribute for Path=[Attribute] for the current index of the ListViewItem.
Psytronic
A: 

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

<CheckBox IsChecked="{Binding Done}"
  Uid="{
    Binding Path=Items.Count,
    Mode=OneTime,
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}
  }"
  Checked="CheckBox_Checked"
  VerticalAlignment="Center"
  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?

Psytronic