Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 610401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:17+00:00 2026-05-13T17:40:17+00:00

I need to dynamically set the contents within the template of a DataGrid based

  • 0

I need to dynamically set the contents within the template of a DataGrid based on information in an external settings file. That settings file specifies which data fields should display in the DataGrid. The administrator of the application can edit the settings to change the fields to display. I cannot hard-code the fields to display.

I can easily add the columns (DataGridTextColumn’s) to the DataGrid at runtime. I set a binding to a field in the item source based on the settings, and that displays fine.

Now I need to display details when the user clicks a row. I set up a RowDetailsTemplate with DataTemplate, and added a Grid (or a StackPanel) inside to format the details. If I add to the markup the TextBlocks with bindings to fields, it displays the details just fine.

But how can I set the content of the Grid/StackPanel in the details template programmatically? The Grid/StackPanel controls are null if I try to reference them by name on startup (e.g., in the page Loaded event). I have tried using the Loaded event on the Grid/StackPanel to add the details. That code runs and appears to add the content to the Grid/StackPanel, but nothing actually appears when I click the row. I’m guessing that the problem is that the template/Grid is already loaded and ignores the changes I’m making.

Here’s a sample of the code I’m using in the handler for the Loaded event. Even if I do something as simple as this, the details pane doesn’t appear when clicking on the row.

<data:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Border Background="LightBlue" >
            <StackPanel x:Name="resultsDetailsPanel"
                Orientation="Vertical"
                Loaded="resultsDetailsPanel_Loaded">
            </StackPanel>
        </Border>
    </DataTemplate>
</data:DataGrid.RowDetailsTemplate>



    private void resultsDetailsPanel_Loaded(object sender, RoutedEventArgs e)
    {
        if (_resultsGridLoaded)
            return;

        StackPanel detailsPanel = sender as StackPanel;

        TextBlock fieldNameTextBlock = new TextBlock();
        fieldNameTextBlock.Text = "TESTING";
        detailsPanel.Children.Add(fieldNameTextBlock);

        _resultsGridLoaded = true;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:40:17+00:00Added an answer on May 13, 2026 at 5:40 pm

    I actually tried your code and is working. Two things you should check:

    • Is your _resultsGridLoaded variable initialized as false?
    • Did you set RowDetailsVisibilityMode=”VisibleWhenSelected” on your DataGrid?

    UPDATE: For some reason is not working anymore. But I did found two ways you can fix it:

    1. Remove the resultsGridLoaded logic.
    2. If you need that logic, you can add a handler for the SelectionChanged event on the DataGrid, in there you can set the _resultsGridLoaded variable to false so the new StackPanel gets its content added correctly:
    
    
        
            
                
                    
                    
                
            
        
    
    
    

    And the code behind:

    private void resultsPanel_Loaded(object sender, RoutedEventArgs e)
    {
        if (_resultsGridLoaded)
            return;
    
        StackPanel pane = (StackPanel)sender;
    
        TextBlock newChild = new TextBlock()
        {
            Text = "New text"
        };
    
        pane.Children.Add(newChild);
    
        _resultsGridLoaded = true;
    }
    
    private void grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _resultsGridLoaded = false;
    }
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to set the text within a DIV element dynamically. What is the
I need to copy the contents of a text file to a dynamically-allocated character
I have a StackPanel that I dynamically change the contents of every 5 minutes
I have a div tag which I populate dynamically. The problem is that in
I need to dynamically create a Video object in ActionScript 2 and add it
Possible Duplicate: Change CSS Dynamically I need to change the height of a div
How do I dynamically reload the app.config in a .net Windows application? I need
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.