Riyal MHH
Riyal MHH

Reputation: 58

How to access a control inside DataGrid.RowDetailsTemplate?

Following is my 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>

My question is how do you access innerGrid DataGrid control from the code. Thanks in Advance.

Upvotes: 2

Views: 2518

Answers (2)

Mohammed Hameed
Mohammed Hameed

Reputation: 93

Thanks kmatyaszek for the right answer. Previously I was trying with RowDetailsTemplate.LoadContent(), which was not updating the UI though able to get the objects.

Here is my updated sample code:

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

Upvotes: 0

kmatyaszek
kmatyaszek

Reputation: 19296

You can access inner DataGrid in LoadingRowDetails event (msdn).

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

    }
}

Upvotes: 1

Related Questions