window1.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WpfApp3
{
class window1:Window
{
private Button button1;
public window1() {
InitializeComponent();
}
private void InitializeComponent() {
//设置窗体
this.Width = 285;
this.Height = 250;
this.Left = this.Top = 100;
this.Title = "code-Only window";
//创建停靠面板对象
DockPanel panal = new DockPanel();
button1 = new Button();
button1.Content = "please click me";
button1.Margin = new Thickness(30);
button1.Click += button1_click;
IAddChild container = panal;
container.AddChild(button1);
//容器等于当前对象
container = this;
container.AddChild(panal);//加上panal面板
}
private void button1_click(object sender,RoutedEventArgs e) {
button1.Content = "thank you";
}
}
}
program.cs
using System;
using System.Windows;
namespace WpfApp3
{
class program:Application
{
[STAThread()] //单线程
static void Main() {
program app = new program();
app.MainWindow = new window1();
app.MainWindow.ShowDialog();
}
}
}