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

Related Questions

I want a spreadsheet function that will produce a sum of all values in
I want a visual GUI designer that will produce XML output in the format
If I want to produce a Base64-encoded output, how would I do that in
I want to have a sweave document that will include a variable number of
I use waf (http://code.google.com/p/waf/) to build a fortran library (which also uses some c-code).
I have a class library assembly and some test code. Because the test code
I want to use gcc to produce a shared library, but i want to
I've written an interface to the code generator that lets me produce shared objects.
i want to protect my Java product by using some USB-based authentication and password
In a view, I want to consume some Json produced by an action on

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.