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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:02:08+00:00 2026-05-17T23:02:08+00:00

We are migrating to Winforms to WPF based solution. We have custom XML definition

  • 0

We are migrating to Winforms to WPF based solution. We have custom XML definition which are used to build the windows form at runtime.

Since XAML is XML based, can we define a HelloWorldWindow.xml file with XAML definition and can it be loaded into the WPF app without any code behind CSharp files? We will attach the code behind hook at runtime.

How to attach the code behind at runtime?

  • 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-17T23:02:09+00:00Added an answer on May 17, 2026 at 11:02 pm

    Create an XML file Tempwin.xml using this XAML

    <UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="Transparent" >
    <Border Background="Black" CornerRadius="10" BorderThickness="4" BorderBrush="RoyalBlue">
    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Text="Sample Text" Foreground="White" Margin="2"></TextBlock>
            <TextBox Grid.Row="1" Margin="5"> </TextBox>
            <TextBlock Text="Sample Text 1" Grid.Row="2" Foreground="White" Margin="2"></TextBlock>
            <TextBox Grid.Row="3" Margin="5"></TextBox>
            <Ellipse Fill="Red" Height="100" Width="100" Grid.Row="4" Margin="0,10,0,0"></Ellipse>
        </Grid>
        </Border>
    

    Create a sample WPF Application with the below xaml

    <Window x:Class="WpfApplication12.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
    
        </Grid.RowDefinitions>
    
        <Button Height="25" Width="100" Margin="2" Click="Button_Click"> Show Content</Button>
        <Grid x:Name="content" Grid.Row="1" Margin="2">
    
        </Grid>
    </Grid>
    

    Paste the below C# code in codebehind the Button_Click

      StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
            FrameworkElement  rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
            content.Children.Add(rootObject);
    

    if you want to load xaml at runtime you cannot give any code behind your XAML file. So i have removed the x:Class attribute before creating the xml

    Events Hooking….

    <UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="Transparent" >
    <Border Background="Black" CornerRadius="10" BorderThickness="4" BorderBrush="RoyalBlue">
    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Text="Sample Text" Foreground="White" Margin="2"></TextBlock>
            <TextBox Grid.Row="1" Margin="5"> </TextBox>
            <TextBlock Text="Sample Text 1" Grid.Row="2" Foreground="White" Margin="2"></TextBlock>
            <TextBox Grid.Row="3" Margin="5"></TextBox>
            <Ellipse Fill="Red" Height="100" Width="100" Grid.Row="4" Margin="0,10,0,0"></Ellipse>
            <Button Grid.Row="5" Height="25" Content="Event added at Runtime" x:Name="btnTest"></Button>
        </Grid>
        </Border>
    

    Button ButtoninXAML;
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
    
            StreamReader mysr = new StreamReader(@"D:\Tempwin.xml");
            FrameworkElement  rootObject = XamlReader.Load(mysr.BaseStream) as FrameworkElement;
            ButtoninXAML = LogicalTreeHelper.FindLogicalNode(rootObject, "btnTest") as Button;
            ButtoninXAML.Click += new RoutedEventHandler(Button_Click1); 
    
            content.Children.Add(rootObject);
    
        }
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Added At Runtime");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am migrating some win forms panels to WPF. In WinForms, you can tab
I am migrating a 1.1 winforms app to 2.0. what are the main things
I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem -
We are migrating our test report data (unit, regression, integration, etc..) from an XML
I have just started migrating my homegrown persistence framework to JPA. Given that the
Currently thinking about pitching the argument for us migration from vs 2005 (winforms) to
Migrating a project from ASP.NET 1.1 to ASP.NET 2.0 and I keep hitting this
I am migrating a struts application from Websphere to Tomcat 6 and my application
I am migrating web apps to new hosting servers, but when I try to
I am migrating an application from .NET 1.1 to .NET 2.0. Should I remove

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.