WPF中如何實(shí)現(xiàn)在一個(gè)彈窗中一個(gè)輸入內(nèi)容的表單,并在父窗口顯示
在wpf開發(fā)中,你有沒有需要用到這樣的場(chǎng)景,比如:在父窗口顯示表單的輸入的內(nèi)容,然后再進(jìn)行一些處理邏輯等,表單可以很復(fù)雜,也可以很簡單,下面我就以示例代碼來做一個(gè)demo展示。
1.父窗口界面展示如下:
<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">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" VerticalAlignment="Bottom" HorizontalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="表單結(jié)果:" VerticalAlignment="Bottom" FontSize="24"></TextBlock>
<TextBox x:Name="UserInputTextBox" Text="{Binding UserInput, Mode=TwoWay}" Width="300" FontSize="24"></TextBox>
</StackPanel>
<Button Grid.Row="1" Content="在新窗口中輸入" VerticalAlignment="Top" Margin="30" Background="LightGray" FontSize="24" Height="60" Command="{Binding InputCommand}"></Button>
</Grid>
</Window>
表單子窗口如下:
<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">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Padding" Value="8"></Setter>
<Setter Property="Height" Value="60"></Setter>
<Setter Property="Width" Value="80"></Setter>
<Setter Property="FontSize" Value="24"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
</Window.Resources>
<DockPanel Margin="10">
<TextBlock x:Name="promptText" Text="請(qǐng)輸入內(nèi)容:" FontSize="24" DockPanel.Dock="Top"></TextBlock>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" DockPanel.Dock="Bottom">
<Button x:Name="OKButton" Content="確定" Click="OKButton_Click"></Button>
<Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" ></Button>
</StackPanel>
<TextBox x:Name="InputBox" TextWrapping="Wrap" Margin="2" FontSize="24"></TextBox>
</DockPanel>
</Window>
表單子窗口后臺(tái)代碼:
/// <summary>
/// UserInputChildrenView.xaml 的交互邏輯
/// </summary>
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;
}
}
2.接口服務(wù)IUserInputService和UserInputService實(shí)現(xiàn):
public interface IUserInputService
{
string RequestInputBox(string prompt, string title, string text = "");
}
實(shí)現(xiàn)類,用來獲取表單輸入的內(nèi)容:
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;
}
}
}
3.新建UserInputViewModel類,通過構(gòu)造函數(shù),注入IUserInputService服務(wù),實(shí)現(xiàn)調(diào)用彈窗表單輸入的子窗口;
public partial class UserInputViewModel : ObservableObject
{
private readonly IUserInputService _userInputService;
public UserInputViewModel(IUserInputService userInputService)
{
_userInputService = userInputService;
}
[ObservableProperty]
private string userInput;
[RelayCommand]
private void Input()
{
var userInput = _userInputService.RequestInputBox("請(qǐng)輸入內(nèi)容:", "用戶輸入", UserInput);
if (!string.IsNullOrEmpty(userInput))
{
UserInput = userInput;
}
}
}
4.服務(wù)注冊(cè)
在App.xaml類中實(shí)現(xiàn)注冊(cè),就可以UserInputViewModel類中使用

5.效果如下:
點(diǎn)擊 “用戶輸入表單”

父窗口如下。點(diǎn)擊“在新窗口中輸入”
在表單中輸入內(nèi)容:你好,點(diǎn)擊確定就可實(shí)現(xiàn);


源代碼地址:https://gitee.com/chenshibao/wpfdemo.git
如果本文介紹對(duì)你有幫助,可以一鍵四連:點(diǎn)贊+評(píng)論+收藏+推薦,謝謝!

浙公網(wǎng)安備 33010602011771號(hào)