RecentComments

Comment RSS

Instantiating a class written in XAML from code.

by Ioannis 17. April 2009 14:14

There are some times when you want to write a specific framgent of the UI in XAML and then intantiate it in a code-behind file. This can also lead to the strange situation of being able to change the UI design completely afer the application has been compiled and shipped.

Here is how such thing can be done:

Suppose that you have written the following XAML code:

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Orientation="Horizontal" Height="50">
<TextBox x:Name="TextBoxTest" Width="100" VerticalAlignment="Center" Margin="5 0 5 0"/>
<Button x:Name="ButtonTest" Content="Click Me" VerticalAlignment="Center"/>
</StackPanel>

This code displays the following:

  

Add a new TextFile to your project and name it "StackPanel.xaml". Write within the file the previous XAML code. Now set its "Build Action" to "Embedded Resource".

In the Window1.xaml write the following lines:

<Window x:Class="XAMLImportTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="TestPanel">
</StackPanel>
</Window>
 

To load the StackPanel.xaml at runtime and display it in the GUI write the following in the Window's constructor (after InitializeComponent()): 

public Window1()
{
InitializeComponent();
System.IO.Stream StackPanelSpectsStream =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("XAMLImportTests.StackPanel.xaml");
StackPanel ContainingItem = System.Windows.Markup.XamlReader.Load(StackPanelSpectsStream) as StackPanel;
 
this.TestPanel.Children.Add(ContainingItem);
}

Setting the StackPanel.xaml as Embedded Resource, effectively hides it within the executable. If you want it available for edit you can set its "Build Action" to "None" and its "Copy to Ouput Directory" to "Copy Always". Substitute the Window1 method's code to the following:

public Window1()
{
InitializeComponent();
 
System.Xml.XmlReader rdr = new System.Xml.XmlTextReader(@".\StackPanel.xaml");
StackPanel ContainingItem = System.Windows.Markup.XamlReader.Load(rdr) as StackPanel;
 
this.TestPanel.Children.Add(ContainingItem);
}
 

Now you can change the GUI after compilation by altering the StackPanel.xaml file. If you want to manipulate the file's controls (such as setting the Text property of the TextBox) you can navigate through the hierarchy of the controls (ContainingItem.Children[0]....):

TextBox Test = ContainingItem.Children[0] as TextBox;
Test.Text = "Hello";
 

kick it on DotNetKicks.com

Shout it

Tags:

WPF

Comments

4/22/2009 12:22:17 PM #

trackback

Trackback from DotNetKicks.com

Instantiating a class written in XAML from code.

DotNetKicks.com

4/22/2009 12:37:01 PM #

trackback

Trackback from DotNetShoutout

C# and .NET Tips and Tricks | Instantiating a class written in XAML from code.

DotNetShoutout

4/27/2009 5:54:39 PM #

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 91 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

Add comment


(Will show your Gravatar icon)

Enter the word
CAPTCHA word
Add 1 to the number above


  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7

Programming Blogs - BlogCatalog Blog Directory Add to Technorati Favorites

MVP Award

Ioannis Panagopoulos





This blog is using BlogEngine.Net and is hosted in the hoster below. I have not experienced any problems installing BlogEngine.Net in the host and I am satisfied with the host's response times. Therefore I recommend it.


DiscountASP Add