情况是这样的!我现在有一个WPF页面的模板!我把它的.xaml里面的内容取出来了。放到一个字符串里了!
想把一些增加控件的代码加到字符串里,然后我就可以重新生成一个WPF的页面,问题是怎么加进去呢?

解决方案 »

  1.   

    给你个例子:PageTemplate.xaml
    <Page x:Class="DynamicControl.PageTemplate"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PageTemplate">
      <StackPanel x:Name="Container">
        
      </StackPanel>
    </Page>Window1.xaml<Window x:Class="DynamicControl.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="600" Width="800">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox x:Name="TextBoxTemplate" Grid.Row="0"></TextBox>
        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
          <Button x:Name="AddButton" Content="Add Button" Click="AddButton_Click" Margin="10"></Button>
          <Button x:Name="AddTextBox" Content="Add TextBox" Click="AddTextBox_Click" Margin="10"></Button>
        </StackPanel>
      </Grid>
    </Window>Window1.xaml.cs using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Xml;
    using System.Windows.Markup;namespace DynamicControl
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                DisplayObject(new PageTemplate());
            }        private void AddButton_Click(object sender, RoutedEventArgs e)
            {
                PageTemplate template = new PageTemplate();
                for (int i = 0; i < 5; ++i)
                {
                    Button button = new Button();
                    button.Name = string.Format("TestTextBox{0}", i);
                    button.Content = string.Format("TestTextBox{0}", i);
                    template.Container.Children.Add(button);
                }
                DisplayObject(template);
            }        private void AddTextBox_Click(object sender, RoutedEventArgs e)
            {
                PageTemplate template = new PageTemplate();
                for (int i = 0; i < 5; ++i)
                {
                    TextBox textbox = new TextBox();
                    textbox.Name = string.Format("TestTextBox{0}", i);
                    textbox.Text = string.Format("TestTextBox{0}", i);
                    template.Container.Children.Add(textbox);
                }
                DisplayObject(template);
            }        private void DisplayObject(object obj)
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = new string(' ', 4);
                settings.NewLineOnAttributes = true;
                StringBuilder strBuild = new StringBuilder();
                XmlWriter xmlwrite = XmlWriter.Create(strBuild, settings);
                XamlWriter.Save(obj, xmlwrite);            TextBoxTemplate.Text = strBuild.ToString();
            }
        }
    }