根据 WPF 中的 TextBox Text 属性启用/禁用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6468160/
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-09-08 22:56:13  来源:igfitidea点击:

Make Button enabled/disabled depending on the TextBox Text property in WPF?

wpfxamldata-binding

提问by Mou

I was looking for code through google that enables and disables a button based on the text property from a textbox. If the textbox has some text then the button should be enabled or otherwise button should be disabled.

我正在通过 google 寻找代码,该代码可以根据文本框中的文本属性启用和禁用按钮。如果文本框有一些文本,则应启用该按钮,否则应禁用该按钮。

I got some code but code in xaml but no c#. I am not being able to understand how it works.

我得到了一些代码,但代码是在 xaml 中,但没有 C#。我无法理解它是如何工作的。

Please help me to understand the behaviour of the following code:

请帮助我理解以下代码的行为:

<TextBox Name="txtName" Width="100" Height="30"></TextBox>

<Button Content="Add Name " Width="100" Height="30" 
  IsEnabled="{Binding ElementName=txtName, Path=Text.Length, Mode=OneWay}"/>

How is the IsEnabled button property working? How is true and false set to the IsEnabled property? What is Path? How does it work?

IsEnabled 按钮属性如何工作?IsEnabled 属性如何设置 true 和 false?什么是路径?它是如何工作的?

采纳答案by CodeNaked

This articleand articleexplain WPF data bindings and how paths work.

这篇文章文章解释了 WPF 数据绑定以及路径的工作原理。

But the binding above will watch the Text property on the TextBox for changes. It will then watch the Length property on the value returned from the Text property, which is a string. The value of the Length property is passed to the IsEnbled property.

但是上面的绑定会监视 TextBox 上的 Text 属性的变化。然后,它会观察从 Text 属性返回的值的 Length 属性,该值是一个字符串。Length 属性的值传递给 IsEnbled 属性。

The value must also be converted, as you are assigning an Int32 (the Length property) to a Boolean (the IsEnabled property). This is generally done via type converters, which is why the above works (which surprises me).

还必须转换该值,因为您要将 Int32(长度属性)分配给布尔值(IsEnabled 属性)。这通常是通过类型转换器完成的,这就是上述工作的原因(这让我感到惊讶)。

UPDATE:

更新:

The first link above no longer works, as MSDN Magazine articles older than 2008 are no longer available online. They are available as CHM files though. The CHM link for the associated article, titled "Data Points: Data Binding in WPF", is:

上面的第一个链接不再有效,因为 2008 年之前的 MSDN 杂志文章不再在线可用。不过,它们可以作为 CHM 文件使用。标题为“数据点:WPF 中的数据绑定”的相关文章的 CHM 链接是:

http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazine2007_12en-us.chm

http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazine2007_12en-us.chm

From MSDN:

来自 MSDN:

2008 and older issues are only available as .chm files. On most versions of windows you must first save these files to your local machine, and then unblock the file in order to read it. To unblock a file, right click on it, and select properties, and then select the ‘unblock' button. The content will then be available to read.

2008 及更早的问题仅作为 .chm 文件提供。在大多数版本的 Windows 上,您必须首先将这些文件保存到本地计算机,然后取消阻止文件以读取它。要取消阻止文件,请右键单击它,然后选择属性,然后选择“取消阻止”按钮。然后内容将可供阅读。

回答by Steve Greatrex

See this postfor an example of how to disable a Buttonbased on TextBoxcontent.

有关如何根据内容禁用 a 的示例,请参阅此帖子ButtonTextBox

Whilst this approach will work, it is better practice to use an ICommand.

虽然这种方法会奏效,但更好的做法是使用ICommand.

In response to comments

回应评论

ElementName= the name of the control in the XAML to which we are binding. The example below would therefore be {Binding ElementName=aName}

ElementName= 我们要绑定到的 XAML 中控件的名称。因此,下面的示例将是{Binding ElementName=aName}

<TextBox x:Name="aName" />

Path= the property path of the value that you want on that element. For example, if you wanted to get the value of the Text property, you would use {Binding Path=Text}. If you wanted to get the length of the Text property, you use {Binding Path.Text.Length}and so on.

Path= 要在该元素上使用的值的属性路径。例如,如果您想获取 Text 属性的值,您可以使用{Binding Path=Text}. 如果你想获得 Text 属性的长度,你可以使用{Binding Path.Text.Length}等等。

Mode=OneWay= the direction in which the binding is evaluated. This is explained here

Mode=OneWay= 评估绑定的方向。这是解释here

回答by Bek Raupov

this is the new Binding syntax in WPF (xaml).

这是 WPF (xaml) 中的新绑定语法。

  1. You are binding to txtName TextBox control/element
  2. You use Pathto specify to what property of the element u r trying to bind to. in this case you are binding to TextBox's Text property and then Text's Length property.
  1. 您正在绑定到 txtName TextBox 控件/元素
  2. 您用于Path指定您尝试绑定到的元素的哪个属性。在这种情况下,您将绑定到 TextBox 的 Text 属性,然后绑定到 Text 的 Length 属性。

So, when textbox is empty. TextBox.Text.Length = 0 which means it will be disabled.

因此,当文本框为空时。TextBox.Text.Length = 0 这意味着它将被禁用。

as soon as you enter any value, it becames >= 0 which gets translated to true.

只要您输入任何值,它就会变为 >= 0 并转换为 true。

i would stronly recommend reading about Binding in WPF before trying to code. It is quite different in WPF compoared to WinForm

我强烈建议在尝试编码之前阅读 WPF 中的绑定。与 WinForm 相比,WPF 有很大不同

回答by Erno

If you do not want to use MVVM/databinding you could:

如果您不想使用 MVVM/数据绑定,您可以:

  1. You could add a DataTrigger to the style of the TextBox and disable/enable the button there.

  2. Or bind the IsEnabled property of the Button to the Text property of the TextBox and use a value converter to turn a string length into a boolean.

  1. 您可以将 DataTrigger 添加到 TextBox 的样式并禁用/启用那里的按钮。

  2. 或者将 Button 的 IsEnabled 属性绑定到 TextBox 的 Text 属性,并使用值转换器将字符串长度转换为布尔值。

回答by Rune Grimstad

Add a property to your view model that calculates the value for IsEnabled and bind to that.

向您的视图模型添加一个属性,用于计算 IsEnabled 的值并绑定到该值。

In the setter for the property that you have bound the TextBox.Text value to you also trigger the changed event for the IsEnabled property.

在已将 TextBox.Text 值绑定到的属性的 setter 中,您还会触发 IsEnabled 属性的更改事件。