只使用代码创建窗口

(1)添加一个类;(记住,不是添加一个窗体)

(2)添加命名空间,using system.windows;

(3)继承window

(4)写代码

using System.Windows;
using System.Windows.Controls;

namespace 只使用代码创建窗体
{
    class Windows1:Window
    {
        private Button button1;

        public Windows1()
        {
            Init();
        }
        void Init()
        {
            this.Title = "只使用代码创建的窗体";
            this.Height = 600;
            this.Width = 600;
            this.Top = 100;
            button1 = new Button();
            button1.Content = "请点击我";
            button1.Margin = new Thickness(10);
            button1.Padding = new Thickness(10);
            button1.Click += button1_click;
            this.AddChild(button1);
        }
        private void button1_click(object sensor,RoutedEventArgs e)
        {
            button1.Content = "已经被点击";
        }
    }
}

效果

猜你喜欢

转载自www.cnblogs.com/riversouth/p/10344916.html