新建wpf项目
新建Window1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace WpfApp2
{
public 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 dockPanel = new DockPanel();
button1 = new Button();
button1.Content = "点我";
button1.Margin = new Thickness(30);
button1.Click += Button1_Click;
IAddChild container = dockPanel;
container.AddChild(this.button1);
container = this;
container.AddChild(dockPanel);
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
button1.Content = "谢谢";
}
}
}
新建Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp2
{
public class Program : Application
{
[STAThread]
static void Main(string[] args)
{
Program app = new Program();
app.MainWindow = new Window1();
app.MainWindow.ShowDialog();
}
}
}