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 313845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:08:44+00:00 2026-05-12T08:08:44+00:00

I want to produce some library code that will be included into WPF applications.

  • 0

I want to produce some library code that will be included into WPF applications. The library may pop up a Window, depending on circumstances. I can define the Window in XAML, but I’d like to treat the XAML as a template. At runtime, at the time the Window is being created so that it can be displayed, I want to replace certain tags in the Xaml template with runtime-defined values.

What I want to do is something like this:

public partial class DynamicXamlWindow : Window
{
    Button btnUpdate = null;
    public DynamicXamlWindow()
    {
        string s = XamlTemplate;

        // replace some things in the XamlTemplate here

        Window root = System.Windows.Markup.XamlReader.Load(...);
        Object _root = this.InitializeFromXaml(new StringReader(s).ReadToEnd()); //??

        btnUpdate = // ???

        //InitializeComponent();
    }

The XamlTemplate string looks like this:

    private string XamlTemplate = @"
    <Window x:Class='@@CLASS'
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            Title='@@TITLE' 
            Height='346' Width='380'>

        <Grid>
          ...

I’ve seen examples where a button or a section is defined in XAML and loaded dynamically. But this is not a button or section. The XamlTemplate provides the XAML for the actual Window.

Is this possible either with InitializeFromXaml or XamlReader.Load ? If so, how?

Can I then retrieve the controls defined in the XAML, for example btnUpdate in the code fragment above. How?

  • 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-12T08:08:45+00:00Added an answer on May 12, 2026 at 8:08 am

    You cannot create a dynamic page that has the x:class attribute. However if the code behind is the same for every dynamic page you can trick it by changing your template to:

    private string XamlTemplate = @"
        <control:BaseWindow
                xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
                xmlns:control='WhateverHere'
                Title='@@TITLE' 
                Height='346' Width='380'>
            <Grid>...
    

    When you are ready to parse this use:

    XamlReader.Parse(xaml);
    

    If you wanted to access items in the code behind you would this.FindName(“btnUpdate”) in the code-behind.

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

Sidebar

Ask A Question

Stats

  • Questions 334k
  • Answers 335k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ah, solved it already :) last version: This is somewhat… May 14, 2026 at 3:30 am
  • Editorial Team
    Editorial Team added an answer You are going off the end of argv by saying… May 14, 2026 at 3:30 am
  • Editorial Team
    Editorial Team added an answer array_merge doesn't work, but '+' does! $a = array(2 =>… May 14, 2026 at 3:30 am

Related Questions

I have a class, Fixture , that I want to cast to a 3rd
I'm not yet a fan of integrated development environments, but I'm trying to overcome
Due to incomplete understanding of eclipse infrastructure I have one problem. Historically we have
What are the implications of running a Microsoft Access Database in both 2003 and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.