tags:

views:

32

answers:

1

I have a DataGrid that has auto generated columns. In side of the AutoColumnsGenerated event I am adding an additional column. This column is a DataGridTemplateColumn which includes a DataTemplate with it's VisualTree set to a CheckBox. I am adding a handler to the CheckBox.ClickEvent in which I will chain a value in the associated row.

If I have multiple DataGrids that are built this way I don't know how to figure out which listview the click event originated from.

From the click event handler I have access to the check box, but its parent is not set. I have also tried using the visual tee helper, but cannot get into the tree in the correct spot.

Does anybody know how I can find out the corresponding DataGrid that contains the clicked CheckBox?

+1  A: 

VisualTreeHelper should work. Try the following code on the event handler:

FrameworkElement fe = sender as FrameworkElement;

while ((fe.GetType() != typeof(DataGrid)) &&
       (fe != null))
{
     fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
}
karmicpuppet
Thank you! I was looping over DependencyObjects instead of FrameworkElements.
Jerod Houghtelling