<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      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類中使用
      image

      5.效果如下:

      點(diǎn)擊 “用戶輸入表單”
      image

      父窗口如下。點(diǎn)擊“在新窗口中輸入”
      image

      在表單中輸入內(nèi)容:你好,點(diǎn)擊確定就可實(shí)現(xiàn);

      image

      image

      源代碼地址:https://gitee.com/chenshibao/wpfdemo.git

      如果本文介紹對(duì)你有幫助,可以一鍵四連:點(diǎn)贊+評(píng)論+收藏+推薦,謝謝!

      posted @ 2025-06-24 23:13  似夢(mèng)亦非夢(mèng)  閱讀(165)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 深夜精品免费在线观看| 亚洲丰满熟女一区二区v| 久久久天堂国产精品女人| 国产成人午夜福利在线观看| 国产精品成人一区二区三| 性色欲情网站iwww| 亚洲精品一区二区动漫| 久久这里都是精品二| 日韩在线视频观看免费网站| 国产亚洲精品久久久久久青梅| 欧美丰满熟妇性xxxx| 久久夜色精品久久噜噜亚| 成人性做爰aaa片免费看| 精品久久亚洲中文无码| 在线a亚洲老鸭窝天堂| 国产精品久久久久久久久久| 2019国产精品青青草原| 精品视频一区二区三区不卡| 天美传媒mv免费观看完整| 亚洲国产日韩一区三区| 亚洲码欧洲码一二三四五| 无码人妻丰满熟妇区毛片18| 盐津县| 亚洲熟妇一区二区三个区| 99久久99这里只有免费费精品| 国产综合精品一区二区三区| 人人爽人人爽人人片av东京热| 熟妇人妻中文a∨无码| 欧美亚洲综合成人A∨在线| 日韩深夜免费在线观看| 国产激情无码一区二区三区| 国色天香成人一区二区 | 国产精品视频午夜福利| 欧美肥老太交视频免费| 日韩高清亚洲日韩精品一区二区| 国产一区二区三区禁18| 亚洲sm另类一区二区三区| 亚洲一区二区精品极品| 久久精品夜夜夜夜夜久久| 亚洲精品一二三四区| 成人国产一区二区三区精品|