gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:46:41 24 4
gpt4 key购买 nike

我正在通过谷歌寻找代码,该代码基于文本框中的文本属性启用和禁用按钮。如果文本框有一些文本,则应启用该按钮,否则应禁用该按钮。

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

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

<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?什么是路径?它是如何工作的?

最佳答案

这个articlearticle解释 WPF 数据绑定(bind)以及路径如何工作。

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

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

更新:

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

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

来自 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.

关于wpf - 根据WPF中的TextBox Text属性启用/禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6468160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com