如何从DataGridCell或模板内的TextBox获取DataGrid

时间:2015-09-29 13:55:36

标签: c# wpf silverlight datagrid

我有一个DataGrid(ProductsDataGrid),这个DataGrid有一个RowDetailTemplate。这个RowDetailTemplate有另一个DataGrid,第二个DataGrid的列中有一个DataTemplate。我想从TextBox的LostFocus事件中获取第二个DataGrid(WarehouseDataGrid)。

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

我试过

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

我想获取DataGrid(WarehouseDataGrid),但我只是得到了DataGridCell。我也试过DataGridCell.Parent但不是DataGrid。

1 个答案:

答案 0 :(得分:2)

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

GetAncestor是一种扩展方法

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