1

I have a DataGrid(ProductsDataGrid) and this DataGrid has a RowDetailTemplate. This RowDetailTemplate has another DataGrid, and this second DataGrid has a DataTemplate in of of its columns. I want to get this second DataGrid(WarehouseDataGrid) from a LostFocus event of a TextBox.

        <sdk:DataGrid x:Name="ProductsDataGrid">
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn Header="Product Name" />
                        <sdk:DataGridTextColumn Header="Quantity" />
                        <sdk:DataGridTextColumn Header="Value" />
                    </sdk:DataGrid.Columns>
                    <sdk:DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <sdk:DataGrid x:Name="WarehouseDataGrid">
                                <sdk:DataGrid.Columns>
                                    <sdk:DataGridTemplateColumn Header="Warehouse" />
                                    <sdk:DataGridTemplateColumn Header="Quantity">
                                        <sdk:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox x:Name="txtQuantity" LostFocus="txtQuantity_LostFocus" />
                                            </DataTemplate>
                                        </sdk:DataGridTemplateColumn.CellTemplate>
                                    </sdk:DataGridTemplateColumn>
                                </sdk:DataGrid.Columns>
                            </sdk:DataGrid>
                        </DataTemplate>
                    </sdk:DataGrid.RowDetailsTemplate>
                </sdk:DataGrid>

I tried

 private void txtQuantity_LostFocus(object sender, RoutedEventArgs e)
 {
      TextBox txt = sender as TextBox;
      var a = txt.Parent; // returns DataGridCell
 }

I want to get the DataGrid(WarehouseDataGrid) but I just get DataGridCell. Also I tried DataGridCell.Parent but isn't DataGrid.

Murilo
  • 1,112
  • 1
  • 18
  • 33
  • 1
    why not try traversing the tree up? Focus on traversing through ancestors: http://stackoverflow.com/questions/4428130/silverlight-visual-tree-manipulation – SOReader Sep 29 '15 at 14:00
  • @SOReader It worked. I had tried using some tree helpers, but I didn't notice they were descendents, in this case it was ancestors. – Murilo Sep 29 '15 at 14:30
  • great to hear that ;) – SOReader Sep 29 '15 at 15:06

1 Answers1

3
private void txtQuantity_LostFocus(object sender, RoutedEventArgs e)
{
    var warehouseDataGrid = ((TextBox)sender).GetAncestor<DataGrid>();
    ... // do stuff
}

GetAncestor is an extension method

public static class ControlExtensions
{
    public static TAncestor GetAncestor<TAncestor>( this DependencyObject subElement )
        where TAncestor : DependencyObject
    {
        return subElement.GetAncestor<TAncestor>( potentialAncestorToStopTheSearch: null );
    }

    public static TAncestor GetAncestor<TAncestor>( this DependencyObject subElement, UIElement potentialAncestorToStopTheSearch )
        where TAncestor : DependencyObject
    {
        DependencyObject parent;
        for (DependencyObject subControl = subElement; subControl != null; subControl = parent)
        {
            if (subControl is TAncestor) return (TAncestor) subControl;

            if (object.ReferenceEquals( subControl, potentialAncestorToStopTheSearch )) return null;


            parent = VisualTreeHelper.GetParent( subControl );
            if (parent == null)
            {
                FrameworkElement element = subControl as FrameworkElement;
                if (element != null)
                {
                    parent = element.Parent;
                }
            }
        }
        return null;
    }
}
Martin
  • 5,714
  • 2
  • 21
  • 41
  • thank you for your answer. I solved my problem before you answering by using the answer in http://stackoverflow.com/questions/4428130/ as suggested by @SOReader which is basically your answer. – Murilo Sep 29 '15 at 14:49