在有效的TextBox上启用按钮 [英] Enable Button on valid TextBox

查看:195
本文介绍了在有效的TextBox上启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,我想要有TextBox和Button。只有当TextBox中的文本长度为10时,才应启用按钮。



我正在尝试使用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屋!

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