Murilo
Murilo

Reputation: 1152

How to get DataGrid from DataGridCell or from TextBox inside template

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.

Upvotes: 1

Views: 1894

Answers (1)

Martin
Martin

Reputation: 6146

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

Upvotes: 3

Related Questions