by Agnaldo
6. junho 2010 15:55
//Window1.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.IO;
namespace _003_ParseOnTheFly
{
class Window1 : Window
{
private Button button1;
public Window1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Width = this.Height = 285;
this.Left = this.Top = 100;
this.Title = "XAML carregado dinamicamente";
FileStream s = new FileStream("Controles.xml", FileMode.Open);
DependencyObject rootElement = (DependencyObject)XamlReader.Load(s);
this.Content = rootElement;
button1 = (Button)LogicalTreeHelper.FindLogicalNode(rootElement,
"button1");
button1.Click += button1_Click;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
button1.Content = "é obediente, mesmo...";
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<!--Controles.xml-->
<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Button Name="button1" Margin="30">Clique aqui...</Button>
</DockPanel>
//Program.cs
using System.Windows;
using System;
namespace _003_ParseOnTheFly
{
public class Program : Application
{
[STAThread()]
public static void Main()
{
Program app = new Program();
app.MainWindow = new Window1();
app.MainWindow.ShowDialog();
}
}
}