WPF如何弹出窗口

本文详细介绍了在WPF工程中如何创建并弹出自定义窗口的步骤,包括自定义窗口的XAML和C#代码实现,以及如何设置窗口的属性如标题和父窗口关系,最终通过ShowDialog()或Show()方法实现窗口的弹出。

Title: WPF如何弹出窗口
Author:Kagula
Date:2013-12-09

LastUpdateDate: 2020-02-11

 

测试环境:
[1]VS2010SP1, VS2019 Community
[2]WPF(.NET Framework 4)项目, .Net Core3.1

内容简介
      WPF工程如何弹出自定义窗口

第一步:自定义个窗口
为当前项目新添个Window项,XAML部份的代码略
,下面是C#部份的代码。

namespace WorkflowBuilder.MyWindows
{
    /// <summary>
    /// Interaction logic for InputStringWindow.xaml
    /// </summary>
    public partial class InputStringWindow : Window
    {
        public InputStringWindow()
        {
            //WindowStartupLocation = WindowStartupLocation.CenterScreen;//屏幕居中
            WindowStartupLocation = WindowStartupLocation.CenterOwner;//在父窗口中居中
            InitializeComponent();

            //设置默认输入焦点
            FocusManager.SetFocusedElement(this,tbContent);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tbContent.Text = tbContent.Text.Trim();

            if (tbContent.Text.Length > 0)
            {
                Close();//关闭窗口
            }
            else
            {
                MessageBox.Show("输入的字符串长度不能为空!");
            }
        }
    }
}


第二步:弹出刚才定义的窗口

 

            InputStringWindow isw = new InputStringWindow();
            isw.Title = "给新页面命名";
            isw.Owner = this;//设置父窗口,这样可以在父窗口中居中
            isw.ShowDialog();//模式,弹出!
            //isw.Show()//无模式,弹出!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值