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

1405

被折叠的 条评论
为什么被折叠?



