如何访问DataGrid.RowDetailsTemplate中的控件?

时间:2013-07-13 08:46:35

标签: c# wpf xaml datagrid rowdetailstemplate

以下是我的XAML:

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <DataGrid Name="innerGrid" IsReadOnly="True" AutoGenerateColumns="False" Margin="10,10,5,5" Width="400" HorizontalAlignment="Left">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Ref" Binding="{Binding Id}"/>
                <DataGridTextColumn  Header="Investor" Binding="{Binding FundProvider.FullName}"/>
                <DataGridTextColumn  Header="Amount" Binding="{Binding InvestmentAmount}"/>
            </DataGrid.Columns>
        </DataGrid>                                                 
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

我的问题是如何从代码中访问innerGrid DataGrid控件。在此先感谢。

2 个答案:

答案 0 :(得分:1)

您可以访问DataGrid事件(msdn)中的内部LoadingRowDetails

private void outerGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
    DataGrid innerGrid = e.DetailsElement as DataGrid;
    if (innerGrid != null)
    {

    }
}

答案 1 :(得分:0)

感谢kmatyaszek的正确答案。 以前我在尝试使用RowDetailsTemplate.LoadContent(),虽然能够获取对象但是没有更新UI。

这是我更新的示例代码:

    void gridEmployee_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
    {
        Border border = e.DetailsElement as Border;

        if (border != null)
        {
            foreach (var grid in border.GetVisualChildren())
            {
                Grid grid_ = grid as Grid;

                if (grid_ != null)
                {
                    foreach (var textBlock in grid_.GetVisualChildren())
                    {
                        TextBlock textBlock_ = textBlock as TextBlock;

                        if (textBlock_ != null && textBlock_.Text == "City : ")
                        {
                            textBlock_.Text = "My assigned text...";
                        }
                    }
                }
            }
        }
    }