(1)子窗口界面设置
<Window x:Class="WpfApplication1.InputWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="输入窗口" Height="101" Width="294" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> <Grid> <Button Content="确定" Height="23" HorizontalAlignment="Right" Margin="0,35,8,0" Name="btnOk" VerticalAlignment="Top" Width="75" Click="btnOk_Click" /> <Button Content="取消" Height="23" HorizontalAlignment="Left" Margin="114,35,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" Click="btnCancel_Click" /> <TextBox Height="32" HorizontalAlignment="Left" Name="txtUserName" VerticalAlignment="Top" Width="278" /> </Grid> </Window>
(2)子窗口属性设置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// InputWindow.xaml 的交互逻辑 /// </summary> public partial class InputWindow : Window { public string InputValue { set; get; } public InputWindow() { InitializeComponent(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; //如果窗口是用ShowDialog打开的,则给dialogresult赋值就会自动关闭窗口,并把结果值返回 } private void btnOk_Click(object sender, RoutedEventArgs e) { InputValue = txtUserName.Text; DialogResult = true; //如果窗口是用ShowDialog打开的,则给dialogresult赋值就会自动关闭窗口,并把结果值返回 } } }
(3)主界面设计
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnOpen_Click(object sender, RoutedEventArgs e) { InputWindow inputWin = new InputWindow(); bool? b = inputWin.ShowDialog(); if (b == null) { MessageBox.Show("没设置?"); }else if(b == true){ MessageBox.Show("确定"+inputWin.txtUserName); }else{ MessageBox.Show("取消"); } } } }