这个是项目结构
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApplication5
{
class Program : Application
{
[STAThread()]
static void Main()
{
Program app = new Program();
app.MainWindow = new Window1();
app.MainWindow.ShowDialog();
}
}
}
Window1.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WpfApplication5
{
class Window1 : Window
{
private Button button1;
public Window1()
{
InitialComponent();
}
private void InitialComponent()
{
this.Width = 300;
this.Height = 300;
this.Left = 100;
this.Top = 100;
this.Title = "Code-Only";
button1 = new Button();
button1.Content = "please click me";
button1.Margin = new Thickness(100);
button1.Click += btn1_Click;//为Button1绑定事件
DockPanel panel = new DockPanel();
IAddChild container = panel;
container.AddChild(button1);
container = this;
container.AddChild(panel);
}
private void btn1_Click(object sender,RoutedEventArgs e)
{
button1.Content = "Thanks you";
}
}
}
运行效果