xaml控件
xaml文件
<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button Name = "button1" Margin = "60">Please Click Me.</Button> </DockPanel>
using System.IO; using System.Windows.Markup; namespace 导入xaml文件控件 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LoadControl("xaml控件.txt"); } public void LoadControl(string xamlFilePath) { Button but; DependencyObject rootElement; using (FileStream fs = new FileStream(xamlFilePath, FileMode.Open)) { rootElement = (DependencyObject)XamlReader.Load(fs); } this.Content = rootElement; but = (Button)LogicalTreeHelper.FindLogicalNode(rootElement, "button1"); but.Click += btn_click; } private void btn_click(object sender,RoutedEventArgs e) { ((Button)sender).Content = "Yes"; } } }