WPF C#输入框 [英] WPF C# InputBox
问题描述
我已经在网上的研究却发现没有关于它的解决方案,我想要什么或不清楚。
I've research online but found no solution to what I want or unclear about it.
我做的使用C#WPF应用程序。我想弹出一个对话框,提示用户输入他/她的名字。从那以后,我将跟踪的名称,并使用该名称的一些数据保存到一个TXT文件。
I am doing a WPF application using C#. I want to pop out a dialog box to prompt user to enter his/her name. After that I will keep track of the name and save some data into a txt file using the name.
例如,
名称输入
NAME =约翰
Name input is name = "John"
所以我有一个数据
数据=1,2,3;
And so I have a data data="1, 2, 3";
然后我保存John.txt文件中的数据。
and then I save the "data" in John.txt file.
任何人都知道该怎么办呢?我认为这个问题是如何弹出一个对话框,用户输入名称。
Anyone know how to do it? I think the problem is how to pop out a dialog for the user to enter name.
感谢您!
推荐答案
我preFER采取使用不锁定的应用程序,并从更传统的Win32对话框移开对话框的方法。
I prefer to take an approach using dialogs that doesn't lock up the application, and moves away from the more traditional Win32 Dialog.
示例
输入对话框隐藏
在这个例子中我使用 MVVM 的基础的解决方案,我用我的应用程序的简化版本。它可能不是pretty,但应该给你它背后的基本知识扎实的想法。
In this example I use a simplified version of the MVVM based solution I am using for my applications. It may not be pretty, but should give you a solid idea on the basics behind it.
的XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/>
<ListBox x:Name="MyListBox"/>
</StackPanel>
<!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
<Grid x:Name="InputBox" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="Orange"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,55,0,55"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" />
<TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/>
<Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Grid>
这很容易显示此对话框,你只需要在输入框
网格的可见性设置为可见。然后,您只需处理是/否按钮,并从文本框输入文本。
It's very easy to show this dialog as you only need to set the Visibility of the InputBox
grid to visible. You then simply handle the Yes / No buttons and get the Input text from the TextBox.
因此,而不是使用code,需要的ShowDialog()
,您只需设置能见度
选项可见
。还有一些事情在这个例子中,我们将在$ C $处理C-背后,例如像处理是/否按钮点击后清零inputText的盒子做的。
So instead of using code that requires ShowDialog()
, you simply set the Visibility
option to Visible
. There are still some things to do in this example that we will handle in code-behind, like for example clearing the InputText box after handling the Yes/No Button clicks.
的code-背后:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CoolButton_Click(object sender, RoutedEventArgs e)
{
// CoolButton Clicked! Let's show our InputBox.
InputBox.Visibility = System.Windows.Visibility.Visible;
}
private void YesButton_Click(object sender, RoutedEventArgs e)
{
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
MyListBox.Items.Add(input); // Add Input to our ListBox.
// Clear InputBox.
InputTextBox.Text = String.Empty;
}
private void NoButton_Click(object sender, RoutedEventArgs e)
{
// NoButton Clicked! Let's hide our InputBox.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Clear InputBox.
InputTextBox.Text = String.Empty;
}
}
}
的$ C $的c-后面可以很容易地用一个依赖完成,或者如在本例视图模型的逻辑,但为简单起见,我保持在$ C $的c-后面
The code-behind could easily be done using a Dependency, or as ViewModel logic in this case, but for simplicity I kept it in the code-behind.
这篇关于WPF C#输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!