WPF C# 输入框
Posted
技术标签:
【中文标题】WPF C# 输入框【英文标题】:WPF C# InputBox 【发布时间】:2011-12-27 13:44:57 【问题描述】:我正在使用 C# 构建 WPF 应用程序。我想弹出一个对话框提示用户输入他/她的名字。之后,我会跟踪名称并将一些数据保存到使用该名称的.txt
文件中。
例如:
姓名输入是 name = "约翰"
所以我有数据 数据="1, 2, 3";
然后我将“数据”保存在John.txt
文件中。
有人知道怎么做吗?
我认为问题在于如何弹出一个对话框让用户输入姓名。
【问题讨论】:
看看social.msdn.microsoft.com/Forums/en/winforms/thread/…。 Simple, quick way to get user input in WPF?的可能重复 当您获得第二个名为“John”的用户时会发生什么? 【参考方案1】:我更喜欢使用不会锁定应用程序的对话框,并远离更传统的 Win32 对话框。
示例
输入对话框隐藏
在此示例中,我使用了基于 MVVM 的解决方案的简化版本,我正在用于我的应用程序。它可能不漂亮,但应该让您对它背后的基础有一个扎实的了解。
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>
</Window>
显示此对话框非常容易,因为您只需将InputBox
网格的可见性设置为可见。然后,您只需处理是/否按钮并从 TextBox 中获取输入文本。
因此,您只需将Visibility
选项设置为Visible
,而不是使用需要ShowDialog()
的代码。在这个例子中还有一些事情要做,我们将在代码隐藏中处理,例如在处理是/否按钮点击后清除 InputText 框。
代码隐藏:
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;
代码隐藏可以使用依赖关系轻松完成,或者在本例中作为 ViewModel 逻辑,但为简单起见,我将其保留在代码隐藏中。
【讨论】:
是的,我明白了。谢谢! 如何防止“InputBox”控件在显示时失去焦点? @Marc 我已经为我的对话框创建了一个基于 ContentControl 元素的自定义控件,在里面我覆盖了 OnVisibleChanged,如果对话框可见,我设置了Keyboard.Focus(textBox);
。
@eandersson 好的,但你如何防止失去焦点?当控件可见并且用户多次按下 tab 时,焦点将被赋予该用户控件后面的控件。我想防止这种情况发生。它应该保留在用户控制范围内。
@eandersson 我通过禁用 (IsEnabled=false) 所有底层控件解决了这个问题。【参考方案2】:
这是我的解决方案。它是完全可定制的。
string inputRead = new InputBox("text").ShowDialog();
或者,如果你愿意的话
string inputRead= new InputBox("Insert something", "Title", "Arial", 20).ShowDialog()
这是类的代码
public class InputBox
Window Box = new Window();//window for the inputbox
FontFamily font = new FontFamily("Tahoma");//font for the whole inputbox
int FontSize=30;//fontsize for the input
StackPanel sp1=new StackPanel();// items container
string title = "InputBox";//title as heading
string boxcontent;//title
string defaulttext = "Write here your name...";//default textbox content
string errormessage = "Invalid answer";//error messagebox content
string errortitle="Error";//error messagebox heading title
string okbuttontext = "OK";//Ok button content
Brush BoxBackgroundColor = Brushes.GreenYellow;// Window Background
Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background
bool clicked = false;
TextBox input = new TextBox();
Button ok = new Button();
bool inputreset = false;
public InputBox(string content)
try
boxcontent = content;
catch boxcontent = "Error!";
windowdef();
public InputBox(string content,string Htitle, string DefaultText)
try
boxcontent = content;
catch boxcontent = "Error!";
try
title = Htitle;
catch
title = "Error!";
try
defaulttext = DefaultText;
catch
DefaultText = "Error!";
windowdef();
public InputBox(string content, string Htitle,string Font,int Fontsize)
try
boxcontent = content;
catch boxcontent = "Error!";
try
font = new FontFamily(Font);
catch font = new FontFamily("Tahoma");
try
title = Htitle;
catch
title = "Error!";
if (Fontsize >= 1)
FontSize = Fontsize;
windowdef();
private void windowdef()// window building - check only for window size
Box.Height = 500;// Box Height
Box.Width = 300;// Box Width
Box.Background = BoxBackgroundColor;
Box.Title = title;
Box.Content = sp1;
Box.Closing += Box_Closing;
TextBlock content=new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Background = null;
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Text = boxcontent;
content.FontFamily = font;
content.FontSize = FontSize;
sp1.Children.Add(content);
input.Background = InputBackgroundColor;
input.FontFamily = font;
input.FontSize = FontSize;
input.HorizontalAlignment = HorizontalAlignment.Center;
input.Text = defaulttext;
input.MinWidth = 200;
input.MouseEnter += input_MouseDown;
sp1.Children.Add(input);
ok.Width=70;
ok.Height=30;
ok.Click += ok_Click;
ok.Content = okbuttontext;
ok.HorizontalAlignment = HorizontalAlignment.Center;
sp1.Children.Add(ok);
void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e)
if(!clicked)
e.Cancel = true;
private void input_MouseDown(object sender, MouseEventArgs e)
if ((sender as TextBox).Text == defaulttext && inputreset==false)
(sender as TextBox).Text = null;
inputreset = true;
void ok_Click(object sender, RoutedEventArgs e)
clicked = true;
if (input.Text == defaulttext||input.Text == "")
MessageBox.Show(errormessage,errortitle);
else
Box.Close();
clicked = false;
public string ShowDialog()
Box.ShowDialog();
return input.Text;
希望对你有用。
【讨论】:
【参考方案3】:只需在您的 Visual Studio 项目中创建另一个 Window 类,该类将用户名保存在公共属性中。然后在主窗口的某处创建此窗口的实例,并使用 ShowDialog 方法显示它。这会阻塞,直到您的“对话框”窗口关闭。然后,您可以从公共属性中获取用户名并使用它做任何您想做的事情。
【讨论】:
【参考方案4】:在您的项目中创建/添加一个新的Window
以获取用户的输入。然后您可以使用Window.Show
或Window.ShowDialog
将该窗口显示为弹出窗口
还添加一个 OK
按钮 n 创建窗口,然后单击确定按钮将信息保存在文本文件中
【讨论】:
【参考方案5】:MSDN 上的自定义对话框部分可能会给您一些指导:Custom Dialog Box in WPF。还有代码示例和 XAML 源代码。
处理完之后,您可以搜索如何将数据保存到文件 - 这相当简单,并且有多种方法可以做到这一点(其中一种是使用 TextWriter
类:example)。
【讨论】:
【参考方案6】:谢谢!!我的修改版:
public class InputBox
Window Box = new Window();//window for the inputbox
FontFamily font = new FontFamily("Avenir");//font for the whole inputbox
int FontSize = 14;//fontsize for the input
StackPanel sp1 = new StackPanel();// items container
string title = "Dica s.l.";//title as heading
string boxcontent;//title
string defaulttext = "";//default textbox content
string errormessage = "Datos no válidos";//error messagebox content
string errortitle = "Error";//error messagebox heading title
string okbuttontext = "OK";//Ok button content
string CancelButtonText = "Cancelar";
Brush BoxBackgroundColor = Brushes.WhiteSmoke;// Window Background
Brush InputBackgroundColor = Brushes.Ivory;// Textbox Background
bool clickedOk = false;
TextBox input = new TextBox();
Button ok = new Button();
Button cancel = new Button();
bool inputreset = false;
public InputBox(string content)
try
boxcontent = content;
catch boxcontent = "Error!";
windowdef();
public InputBox(string content, string Htitle, string DefaultText)
try
boxcontent = content;
catch boxcontent = "Error!";
try
title = Htitle;
catch
title = "Error!";
try
defaulttext = DefaultText;
catch
DefaultText = "Error!";
windowdef();
public InputBox(string content, string Htitle, string Font, int Fontsize)
try
boxcontent = content;
catch boxcontent = "Error!";
try
font = new FontFamily(Font);
catch font = new FontFamily("Tahoma");
try
title = Htitle;
catch
title = "Error!";
if (Fontsize >= 1)
FontSize = Fontsize;
windowdef();
private void windowdef()// window building - check only for window size
Box.Height = 100;// Box Height
Box.Width = 450;// Box Width
Box.Background = BoxBackgroundColor;
Box.Title = title;
Box.Content = sp1;
Box.Closing += Box_Closing;
Box.WindowStyle = WindowStyle.None;
Box.WindowStartupLocation = WindowStartupLocation.CenterScreen;
TextBlock content = new TextBlock();
content.TextWrapping = TextWrapping.Wrap;
content.Background = null;
content.HorizontalAlignment = HorizontalAlignment.Center;
content.Text = boxcontent;
content.FontFamily = font;
content.FontSize = FontSize;
sp1.Children.Add(content);
input.Background = InputBackgroundColor;
input.FontFamily = font;
input.FontSize = FontSize;
input.HorizontalAlignment = HorizontalAlignment.Center;
input.Text = defaulttext;
input.MinWidth = 200;
input.MouseEnter += input_MouseDown;
input.KeyDown += input_KeyDown;
sp1.Children.Add(input);
ok.Width = 70;
ok.Height = 30;
ok.Click += ok_Click;
ok.Content = okbuttontext;
cancel.Width = 70;
cancel.Height = 30;
cancel.Click += cancel_Click;
cancel.Content = CancelButtonText;
WrapPanel gboxContent = new WrapPanel();
gboxContent.HorizontalAlignment = HorizontalAlignment.Center;
sp1.Children.Add(gboxContent);
gboxContent.Children.Add(ok);
gboxContent.Children.Add(cancel);
input.Focus();
void Box_Closing(object sender, System.ComponentModel.CancelEventArgs e)
//validation
private void input_MouseDown(object sender, MouseEventArgs e)
if ((sender as TextBox).Text == defaulttext && inputreset == false)
(sender as TextBox).Text = null;
inputreset = true;
private void input_KeyDown(object sender, KeyEventArgs e)
if (e.Key == Key.Enter && clickedOk == false )
e.Handled = true;
ok_Click(input, null);
if (e.Key == Key.Escape)
cancel_Click(input, null);
void ok_Click(object sender, RoutedEventArgs e)
clickedOk = true;
if (input.Text == defaulttext || input.Text == "")
MessageBox.Show(errormessage, errortitle,MessageBoxButton.OK,MessageBoxImage.Error);
else
Box.Close();
clickedOk = false;
void cancel_Click(object sender, RoutedEventArgs e)
Box.Close();
public string ShowDialog()
Box.ShowDialog();
return input.Text;
【讨论】:
以上是关于WPF C# 输入框的主要内容,如果未能解决你的问题,请参考以下文章