Jerod Houghtelling
Jerod Houghtelling

Reputation: 4867

Get containing row from a CheckBox inside of a DataGrid

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?

Upvotes: 0

Views: 913

Answers (1)

ASanch
ASanch

Reputation: 10373

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;
}

Upvotes: 3

Related Questions