多窗口实例:
1.在多个窗口的情况下如何自定义指定要启动的窗口程序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Application x:Class="toolbar.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 StartupUri="MainWindow.xaml"> 5 <Application.Resources> 6 7 </Application.Resources> 8 </Application>
可以通过StartupUri="MainWindow.xaml" 修改会你要启动的窗口名称即可
2.固定窗口大小,禁止修改
ResizeMode="NoResize"
3.对话框弹出的时候在屏幕显示的位置
WindowStartupLocation="CenterScreen"
4.修改窗口名称
Title="关于"
5.窗口最大化
WindowState="Maximized"
6.给对话框传值
其实窗口就是一个类
public partial class AboutWindow : Window
这样我们就可以在类里定义个公共属性用于接收值,在这之前new一个实例
把值传给这个类里的属性,最后在子窗口的load事件去接受
public partial class AboutWindow : Window { public string UserName { get; set; } public string Pwd { get; set; } ...
主窗口
using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace toolbar { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click_1(object sender, RoutedEventArgs e) { string t = tx.Text; AboutWindow aboutWin = new AboutWindow(); aboutWin.UserName = t; aboutWin.ShowDialog(); MessageBox.Show(aboutWin.Pwd); } } }
子窗口
private void Window_Loaded_1(object sender, RoutedEventArgs e) { re.Text = UserName; }
7.子窗口返回属性
无论是子窗口向主窗口传值还是主.向.子传值,都是通过属性
8.OpenFileDialog
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click_1(object sender, RoutedEventArgs e) { OpenFileDialog file = new OpenFileDialog(); //文件过滤器 file.Filter = "文本文件|*.txt|PNG图片|*.png|视频|*.avi"; file.ShowDialog(); } }