在有效的TextBox上启用按钮 [英] Enable Button on valid TextBox
问题描述
我正在尝试使用Binding进行此操作,但不会触发 IsValid
属性在我在TextBox中输入时读取。
如何做?
如果你只有这种依赖关系和一个有效的值,你可以使用一个样式:
< TextBox Name =tbTest/>
< Button Content =Do Stuff>
< Button.Style>
< Style TargetType ={x:Type Button}>
< Setter Property =IsEnabledValue =False/>
< Style.Triggers>
< DataTrigger Binding ={Binding ElementName = tbTest,Path = Text.Length}Value =10>
< Setter Property =IsEnabledValue =true/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Button.Style>
< / Button>
否则你应该定义一个 ValidationRule
应该添加到文本绑定。然后,您可以使用代码隐藏来检查它是否有效,或将 IsEnabled
属性绑定到 Validation.HasError
(使用 ValueConverter
反转布尔值)。
In WPF, I would like to have TextBox and Button. Button should be enabled only if the length of the text in TextBox is 10.
I am trying to do this using Binding, but it doesn't fire IsValid
property reads as I type in the TextBox.
How can this be done?
If you have just this dependency and one value for which it is valid you can use a style:
<TextBox Name="tbTest"/>
<Button Content="Do Stuff">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=tbTest, Path=Text.Length}" Value="10">
<Setter Property="IsEnabled" Value="true"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Otherwise you should define a ValidationRule
which should be added to the Text-binding. Then you can use code-behind to check if it is valid or bind the IsEnabled
property to Validation.HasError
(using a ValueConverter
to invert the boolean).
这篇关于在有效的TextBox上启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!