C# 在 WPF 中单击按钮后如何清除文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16236392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:28:55  来源:igfitidea点击:

How to clear a textbox once a button is clicked in WPF?

c#wpfxamlbuttontextbox

提问by Anjola

How can I clear a textboxonce a button is clicked in the WPF application, I know I have to do it in click method of the button but what code should I use for the mentioned purpose?

textbox在 WPF 应用程序中单击按钮后如何清除,我知道我必须在按钮的 click 方法中执行此操作,但是我应该使用什么代码来实现上述目的?

采纳答案by ChrisO

Give your textbox a name and then use TextBoxName.Text = String.Empty;

为您的文本框命名,然后使用 TextBoxName.Text = String.Empty;

回答by devdigital

You wouldn't have to put it in the button click hander. If you were, then you'd assign your text box a name (x:Name) in your view and then use the generated member of the same name in the code behind to set the Textproperty.

您不必将它放在按钮单击处理程序中。如果是,那么您可以x:Name在视图中为文本框指定一个名称 ( ),然后在后面的代码中使用生成的同名成员来设置该Text属性。

If you were avoiding code behind, then you would investigate the MVVM design pattern and data binding, and bind a property on your view model to the text box's Textproperty.

如果您要避免代码隐藏,那么您将研究 MVVM 设计模式和数据绑定,并将视图模型上的属性绑定到文本框的Text属性。

回答by misak

For example:

例如:

XAML:

XAML:

<Button Content="ok" Click="Button_Click"/>
<TextBlock Name="textBoxName"/>

In code:

在代码中:

 private void Button_Click(object sender, RoutedEventArgs e)
{
textBoxName.Text = "";
}

回答by misak

I use this. I think this is the simpliest way to do it:

我用这个。我认为这是最简单的方法:

 textBoxName.Clear();

回答by Er. Harry Singh

When you run your form and you want showing text in textbox is clear so you put the code : -

当您运行表单并希望在文本框中显示文本时很清楚,因此您可以输入代码:-

textBox1.text = String.Empty;

Where textBox1 is your textbox name.

其中 textBox1 是您的文本框名称。

回答by Nio74

For me texBoxName.Clear();is the best method because I have textboxs in binding and if I use other methods I do not have a good day

对我来说texBoxName.Clear();是最好的方法,因为我有绑定文本框,如果我使用其他方法,我的日子不好过

回答by Farhan Aslam

You can use Anyof the statement given below to clear the text of the text box on button click:

您可以使用下面给出的任何语句来清除按钮单击时文本框的文本:

  1. textBoxName.Text = string.Empty;
  2. textBoxName.Clear();
  3. textBoxName.Text = "";
  1. textBoxName.Text = string.Empty;
  2. textBoxName.Clear();
  3. textBoxName.Text = "";

回答by Tomá? Oplatek

There is one possible pitfall with using textBoxName.Text = string.Empty;and that is if you are using Text binding for your TextBox (i.e. <TextBox Text="{Binding Path=Description}"></TextBox>). In this case, setting an empty string will actually override and break your binding.

使用存在一个可能的陷阱,textBoxName.Text = string.Empty;那就是如果您为 TextBox(即<TextBox Text="{Binding Path=Description}"></TextBox>)使用文本绑定。在这种情况下,设置空字符串实际上会覆盖并破坏您的绑定。

To prevent this behavior you have to use the Clear method:

为了防止这种行为,您必须使用 Clear 方法:

textBoxName.Clear();

This way the TextBox will be cleared, but the binding will be kept intact.

这样 TextBox 将被清除,但绑定将保持不变。