情况是这样的!我现在有一个WPF页面的模板!我把它的.xaml里面的内容取出来了。放到一个字符串里了!
想把一些增加控件的代码加到字符串里,然后我就可以重新生成一个WPF的页面,问题是怎么加进去呢?
想把一些增加控件的代码加到字符串里,然后我就可以重新生成一个WPF的页面,问题是怎么加进去呢?
解决方案 »
- VS2008开发的dll程序,调试时启动VS2010调用dll,无法进入调试,提示调试器的协议与调试对象不兼容
- C#窗体 webBrowser控件赋值后无法编辑~~~~~
- C#操作word 插入动态表格相关问题!!!
- 开发简单的象棋程序,需要设计哪些类?我如下设计如何,给点建议?
- 如何在服务器端实现?
- 请问如何在form里控制WebBrowser 点击链接不弹出IE窗口,直接在我的form里打开??
- vs2005---ToolStrip中button的属性和statusStrip中的statusLable属性
- 获取桌面上的图片信息时出错
- 求救:如何将WINDOWS应用程序集成在WEB应用页内,以解决WINDOWS应用程序的发布问题!谢谢!急!在线等!
- C#三层架构自动生成的问题
- 怎样判断窗体的类型
- 按钮点击
<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();
}
}
}