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

  • Home
  • SEARCH
  • 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 6914199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:59+00:00 2026-05-27T09:18:59+00:00

I am working on a project in C# and WPF that dynamically creates a

  • 0

I am working on a project in C# and WPF that dynamically creates a grid layout that has some text, an image, and a button. I create many of these grids, and add them to a stack panel. I create this grid using the XamlReader.Create() function. Here is the whole code, and i apologize that the Xaml code is so long

string xaml = "<Grid Height='92' Margin='0,10,0,0' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"+
            "<Grid.Background>"+
                "<LinearGradientBrush EndPoint='0.5,1' StartPoint='0.5,0'>"+
                    "<GradientStop Color='#FFACD7FF' Offset='0.172'/>"+
                    "<GradientStop Color='#FFACD7FF' Offset='0.508'/>"+
                    "<GradientStop Color='#FF69B5FF' Offset='0.996'/>"+
                    "<GradientStop Color='#FF79BEFB' Offset='0.017'/>"+
                    "<GradientStop Color='#FFACD7FF' Offset='0.877'/>"+
                "</LinearGradientBrush>"+
            "</Grid.Background>"+
            "<Grid.RowDefinitions>"+
                "<RowDefinition Height='0.522*'/>"+
                "<RowDefinition Height='0.478*'/>"+
            "</Grid.RowDefinitions>"+
            "<Grid.ColumnDefinitions>"+
                "<ColumnDefinition Width='133'/>"+
                "<ColumnDefinition Width='73.5'/>"+
                "<ColumnDefinition Width='0.833*'/>"+
                "<ColumnDefinition Width='0.167*'/>"+
                "<ColumnDefinition Width='108.775'/>"+
            "</Grid.ColumnDefinitions>"+
            "<Image Source='stockHome.jpg' Margin='8' Grid.Column='4' Grid.RowSpan='2'/>"+
            "<TextBlock TextWrapping='Wrap' Text='Details: "+
            addHouse.description+
            "' Margin='8' Grid.Column='2' HorizontalAlignment='Center'/>"+
            "<Button Content='View Full Description' Grid.Column='2' HorizontalAlignment='Center' Margin='4,8,0,8' Grid.Row='1' Width='134.667'/>"+
            "<Label Content='$"+
            addHouse.cost+
            "' Margin='8,8,0,0' FontSize='21.333' FontFamily='Segoe UI Semibold' HorizontalAlignment='Left' RenderTransformOrigin='0.53,1.474'/>"+
            "<Label Content='"+
            addHouse.neighborhood+
            "' Margin='8,0,0,8' Grid.Row='1' HorizontalAlignment='Left' Width='117' FontSize='18.667'/>"+
            "<TextBlock Grid.Column='1' Margin='8,15,8,8' Grid.RowSpan='2' TextWrapping='Wrap'><Run Text='"+
            addHouse.type+
            "'/><LineBreak/><Run Text='"+
            addHouse.bedrooms+
            " Bed'/><LineBreak/><Run Text='"+
            addHouse.bathrooms+
            " Bath'/><LineBreak/><Run Text='ID: "+
            addHouse.ID+
            "'/></TextBlock>"+
            "<Image Grid.Column='3' Margin='8,23.667,8,20.251' Grid.RowSpan='2' Source='HeartPop.png'/>"+
            "</Grid>";  


            // Load the panel
        StringReader stringReader = new StringReader(xaml);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        Grid readerLoadGrid = (Grid)XamlReader.Load(xmlReader);

This has worked fine for me, and i thought this would be the correct way to generate a grid layout at runtime, but now that i need to name the button, and define an event handler, this falls apart. when i try to change the button description to

 <Button x:Name = 'houseButton' Click = 'descriptionButtonClick' ...

I get a XAMLParse exception. After reading about this, i am not so sure that you can declare these things in xaml at runtime. So now, I am very confused on how else to do this. I cant just change the name after i have created the grid, as it has no name to be called by. is there a way to access a child of a UIElement even if it has no name? or is there a way to declare the name in XAML at runtime and im just doing it wrong?

  • 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-27T09:19:00+00:00Added an answer on May 27, 2026 at 9:19 am

    You can define the Grid on runtime like you can do it with most other controls. here is a page showing an example how:

    http://sullivan.net/blog/2009/09/creating-a-wpf-grid-programmatically-code-behind/

    than you’ll be able to use button objects and access them using c# like:

    Button myBtn = new Button();
    Button.Click += new ….

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

Sidebar

Related Questions

I'm working on a multi-lingual WPF project that will be localized into many different
I am working with C# and a WPF project. I created a button that,
I have a project I'm working on that requires our WPF application read SMS
I have a WPF C# project that I am working on and I have
I am currently working on a WPF/.Net project, In this project the user has
I'm working on a project where I have some hierarchical data that I want
I'm working on a solution where I have a WPF project that is using
I have a small project I am working on which is WPF c# that
Im using pages in the WPF project that im currently working on. However i
I was working on a WPF project today that had a main nav window

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.