根据WPF中的TextBox Text属性启用/禁用“启用”按钮? [英] Make Button enabled/disabled depending on the TextBox Text property in WPF?

查看:2965
本文介绍了根据WPF中的TextBox Text属性启用/禁用“启用”按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我得到了一些代码,但是在xaml中却没有c#。我不能理解它是如何工作的。

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

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

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}"/>

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

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

推荐答案

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

This article and article explain WPF data bindings and how paths work.

但是上面的绑定将会在TextBox上观看Text属性以进行更改。然后,它会从Text属性返回的值(其为一个字符串)中查看Length属性。 Length属性的值传递给IsEnbled属性。

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.

此值也必须转换,因为将Int32(Length属性)分配给Boolean( IsEnabled属性)。这通常是通过类型转换器完成的,这就是为什么上述工作(令我惊讶的是)。

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).

更新:

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

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:

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

从MSDN:


2008年和更早版本的问题仅作为.chm文件使用。在大多数Windows版本上,您必须首先将这些文件保存到本地计算机,然后取消阻止该文件才能读取。要解除封锁文件,请右键单击该文件,然后选择属性,然后选择取消阻止按钮。内容随后可以阅读。

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.

这篇关于根据WPF中的TextBox Text属性启用/禁用“启用”按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆