按文本框启用或禁用按钮是否为空。 [英] Enable or disable button by textboxes empty or not.

查看:103
本文介绍了按文本框启用或禁用按钮是否为空。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近被问到一个关于如何在加载View时禁用按钮的问题;然后在最终用户开始在某些文本框中输入数据时再次启用它。对于例如在WPF搜索应用程序中,仅当最终用户在搜索文本框中输入某些文本时,搜索按钮才会启用。



我的问题是找到一个文本框的解决方案



但是在我的应用程序中有几个文本框。我想在所有文本框都不为空时启用按钮。如果任何文本框为空,则应禁用该按钮。



链接中的代码仅应用一个属性名称。

 SearchPersonCommnad = new RelayActionCommand()
{
CanExecuteAction = n => !String.IsNullOrEmpty(Name),
ExecuteAction = n => defaultView.Filter = name => ((PersonInfo)name)。FirstName.StartsWith(Name)
|| ((PersonInfo)name).LastName.StartsWith(Name)
|| ((PersonInfo)name).City == Name
};





假设我有很多属性,怎么做那个?



我尝试了什么:



有一个< a href =http://www.dotnetcurry.com/wpf/1130/wpf-commanding-enable-button>类似的。但它没有解决我的问题。

解决方案

如果要在一组属性中的任何一个为空时禁用该命令,那么只需更新 CanExecuteAction 相应地:

 CanExecuteAction = n = >  !string.IsNullOrEmpty(FirstProperty)&& !string.IsNullOrEmpty(SecondProperty)&& ... 


I was recently asked a question on how to disable a button when the View is loaded; and then enable it again when the end-user starts entering data in some textboxes. For e.g. in a WPF Search application, only if the end-user enters some text in the Search textbox, the Search button should become enabled.

My question is I find a solution for one textbox.

However in my application there are several textboxes. I want to enable the button when all textboxes are not empty. If any textbox is empty, then the button should be disabled.

The code in the link only apply one property Name.

SearchPersonCommnad = new RelayActionCommand()
            {
                CanExecuteAction = n=> !String.IsNullOrEmpty(Name),
                 ExecuteAction = n => defaultView.Filter = name => ((PersonInfo)name).FirstName.StartsWith(Name) 
                     || ((PersonInfo)name).LastName.StartsWith(Name) 
                     || ((PersonInfo)name).City==Name
            };



Let's say I have many properties, how to do that?

What I have tried:

There is a similar one. But it doesn't solve my question.

解决方案

If you want to disable the command when any one of a set of properties is empty, then just update the CanExecuteAction accordingly:

CanExecuteAction = n => !string.IsNullOrEmpty(FirstProperty) && !string.IsNullOrEmpty(SecondProperty) && ...


这篇关于按文本框启用或禁用按钮是否为空。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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