新建一个window1的Class类
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace wpf2
{
//继承window
class window1:Window
{
private Button button1;
public window1()
{
InitializedComponent();
}
private void InitializedComponent()
{
//配置窗体大小
this.width = 286;
this.heigth = 250;
this.Letf = this.top = 100;
this.title = "Code-Only window";
//创建停靠面板对象
DockPanel panel = new Dockpanel();
//创建按钮对象
button1 = new Button();
button1.Content = "please click me";
button1.Margin = new Thickness(30);
button1.Click += button_Click;
IAddChild container = panel;
container.AddChild(button1);
container = this;
container.AddChild(panel);
}
private void button1_Click(object sender, RouteEventArges e)
{
button1.Content = ''thank you";
}
}
}
创建一个Properties类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace wpf2
{
class Program:Application
{
//必须用到单线程方法
[STAThread()]
static void Main()
{
Program app = new Program();
app.MainWindow = new window1();
app.MainWindow.ShowDialog();
}
}
}
设置启动对象。