堆儿堆儿打打 發表於 2025-6-24 23:13:00

WPF中如何实现在一个弹窗中一个输入内容的表单,并在父窗口显示

<h1 id="在wpf开发中你有没有需要用到这样的场景比如在父窗口显示表单的输入的内容然后再进行一些处理逻辑等表单可以很复杂也可以很简单下面我就以示例代码来做一个demo展示">在wpf开发中,你有没有需要用到这样的场景,比如:在父窗口显示表单的输入的内容,然后再进行一些处理逻辑等,表单可以很复杂,也可以很简单,下面我就以示例代码来做一个demo展示。</h1>
<h2 id="1父窗口界面展示如下">1.父窗口界面展示如下:</h2>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.UserInputView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                mc:Ignorable="d"
                WindowStartupLocation="CenterScreen"
                Title="UserInputView" Height="450" Width="800"&gt;
        &lt;Grid &gt;
                &lt;Grid.RowDefinitions&gt;
                        &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                        &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                &lt;/Grid.RowDefinitions&gt;
                &lt;StackPanel Grid.Row="0" VerticalAlignment="Bottom" HorizontalAlignment="Center" Orientation="Horizontal"&gt;
                        &lt;TextBlock Text="表单结果:" VerticalAlignment="Bottom" FontSize="24"&gt;&lt;/TextBlock&gt;
                        &lt;TextBox x:Name="UserInputTextBox" Text="{Binding UserInput, Mode=TwoWay}" Width="300" FontSize="24"&gt;&lt;/TextBox&gt;
                &lt;/StackPanel&gt;
                &lt;Button Grid.Row="1" Content="在新窗口中输入" VerticalAlignment="Top" Margin="30" Background="LightGray" FontSize="24" Height="60" Command="{Binding InputCommand}"&gt;&lt;/Button&gt;
        &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>
<p>表单子窗口如下:</p>
<pre><code>&lt;Window x:Class="WPFDemoMVVM.View.UserInputChildrenView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:local="clr-namespace:WPFDemoMVVM.View"
                mc:Ignorable="d"
                WindowStartupLocation="CenterOwner"
                ResizeMode="NoResize"
                WindowStyle="ToolWindow"
                Title="UserInputChildrenView" Height="300" Width="600"&gt;
        &lt;Window.Resources&gt;
                &lt;Style TargetType="Button"&gt;
                        &lt;Setter Property="Padding" Value="8"&gt;&lt;/Setter&gt;
                        &lt;Setter Property="Height" Value="60"&gt;&lt;/Setter&gt;
                        &lt;Setter Property="Width" Value="80"&gt;&lt;/Setter&gt;
                        &lt;Setter Property="FontSize" Value="24"&gt;&lt;/Setter&gt;
                        &lt;Setter Property="Margin" Value="10"&gt;&lt;/Setter&gt;
                &lt;/Style&gt;
        &lt;/Window.Resources&gt;

        &lt;DockPanel Margin="10"&gt;
                &lt;TextBlock x:Name="promptText" Text="请输入内容:" FontSize="24" DockPanel.Dock="Top"&gt;&lt;/TextBlock&gt;
                &lt;StackPanel HorizontalAlignment="Right" Orientation="Horizontal" DockPanel.Dock="Bottom"&gt;
                        &lt;Button x:Name="OKButton" Content="确定" Click="OKButton_Click"&gt;&lt;/Button&gt;
                        &lt;Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" &gt;&lt;/Button&gt;
                &lt;/StackPanel&gt;
                &lt;TextBox x:Name="InputBox" TextWrapping="Wrap" Margin="2" FontSize="24"&gt;&lt;/TextBox&gt;
        &lt;/DockPanel&gt;
&lt;/Window&gt;
</code></pre>
<p>表单子窗口后台代码:</p>
<pre><code>/// &lt;summary&gt;
/// UserInputChildrenView.xaml 的交互逻辑
/// &lt;/summary&gt;
public partial class UserInputChildrenView : Window
{
        public UserInputChildrenView()
        {
                InitializeComponent();
                Owner ??= Application.Current.MainWindow;
                this.InputBox.Focus();
                this.InputBox.SelectAll();
        }

        public string? UserInput { get; private set; }
        public UserInputChildrenView(string prompt, string title, string text = "") : this()
        {
                this.promptText.Text = prompt;
                this.Title = title;
                this.InputBox.Text = text;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
                this.InputBox.Text = this.InputBox.Text.Trim();
                this.DialogResult = true;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
                this.DialogResult = false;
        }
}
</code></pre>
<h2 id="2接口服务iuserinputservice和userinputservice实现">2.接口服务IUserInputService和UserInputService实现:</h2>
<pre><code>public interface IUserInputService
{
        string RequestInputBox(string prompt, string title, string text = "");
}
</code></pre>
<p>实现类,用来获取表单输入的内容:</p>
<pre><code> public class UserInputService : IUserInputService
{
       public string RequestInputBox(string prompt, string title, string text = "")
       {
               var input = new UserInputChildrenView(prompt, title, text);
               var res = input.ShowDialog();
               if (res == true)
               {
                       return input.InputBox.Text;
               }
               else
               {
                       return null;
               }
       }
}
</code></pre>
<h2 id="3新建userinputviewmodel类通过构造函数注入iuserinputservice服务实现调用弹窗表单输入的子窗口">3.新建UserInputViewModel类,通过构造函数,注入IUserInputService服务,实现调用弹窗表单输入的子窗口;</h2>
<pre><code>public partial class UserInputViewModel : ObservableObject
{
        private readonly IUserInputService _userInputService;
        public UserInputViewModel(IUserInputService userInputService)
        {
                _userInputService = userInputService;
        }
       
       
        private string userInput;
       
        private void Input()
        {
                var userInput = _userInputService.RequestInputBox("请输入内容:", "用户输入", UserInput);
                if (!string.IsNullOrEmpty(userInput))
                {
                        UserInput = userInput;
                }
        }
}
</code></pre>
<h2 id="4服务注册">4.服务注册</h2>
<p>在App.xaml类中实现注册,就可以UserInputViewModel类中使用<br>
<img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250624230033187-828099915.png"></p>
<h2 id="5效果如下">5.效果如下:</h2>
<p>点击 “用户输入表单”<br>
<img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250624230323532-1221899566.png"></p>
<p>父窗口如下。点击“在新窗口中输入”<br>
<img src="uploading..."></p>
<p>在表单中输入内容:你好,点击确定就可实现;</p>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250624231103036-1256538099.png"></p>
<p><img src="https://img2024.cnblogs.com/blog/2212230/202506/2212230-20250624231141348-1694202205.png"></p>
<p>源代码地址:https://gitee.com/chenshibao/wpfdemo.git</p>
<p><strong>如果本文介绍对你有帮助,可以一键四连:点赞+评论+收藏+推荐,谢谢!</strong></p><br><br>
来源:https://www.cnblogs.com/chenshibao/p/18945335
頁: [1]
查看完整版本: WPF中如何实现在一个弹窗中一个输入内容的表单,并在父窗口显示