简体   繁体   English

在WPF中,如何根据不同的TextBox Text属性的输入启用/禁用按钮?

[英]In WPF how to Make Button enabled/disabled depending on the input of different TextBox Text property?

How to Make Button enabled/disabled depending on the input of different TextBox Text property using wpf binding ? 如何使用wpf绑定根据不同TextBox Text属性的输入启用/禁用按钮?

Related question: Make Button enabled/disabled depending on the TextBox Text property in WPF? 相关问题:是否根据WPF中的TextBox Text属性启用/禁用按钮?

I saw this question similar but this enabling/disabling depends on one TextBox. 我看到了类似的问题,但是此启用/禁用取决于一个TextBox。

Is there any way to bind a button enabling/disabling with more than one TextBox Text properties ? 有什么方法可以绑定具有多个TextBox Text属性的启用/禁用按钮? Specifically, need to disable/enable the button based on a ItemsControl containing a list of TextBox ? 具体来说,是否需要基于包含TextBox列表的ItemsControl禁用/启用按钮?

You can follow these simple steps, I do not wanted to write the whole code by myself but feel free to ask in case you find it difficult: 您可以按照以下简单步骤进行操作,我不想自己编写整个代码,但是如果您觉得困难,可以随时询问:
1. Based on the linked question, Set the ElementName as the ItemsControlName . 1.根据链接的问题,将ElementName设置为ItemsControlName
2. Create a converter that takes the control as value . 2.创建一个将控制作为value的转换器。 In the convert() , check the conditions on the Items of the ItemsControl (value) & return disabled/Enabled(False/true) from the convert() . convert() ,检查ItemsControl的Items条件(值),并从convert()返回disable / Enabled(False / true convert()
3.Create an instance of the converter with a key inside Windows.Resources . 3.使用Windows.Resources的密钥创建converter的实例。
4. Add the converter to the IsEnabled="{ElementName=ItemsControlName,Path={Binding},Converter={StaticResource convKey}}" . 4.将转换器添加到IsEnabled="{ElementName=ItemsControlName,Path={Binding},Converter={StaticResource convKey}}"
You might get some syntactical errors,please correct them & give it a try. 您可能会遇到一些语法错误,请更正并尝试一下。

I would like to approach this problem in an MVVM way. 我想以MVVM方式解决此问题。

Usually when working with a Button you set the Command property in XAML... 通常在使用Button您需要在XAML中设置Command属性。

<Button Command="{Binding MyCommand}" Content="Click Me"/>

and in your view model you have a ICommand associated with it. 在您的视图模型中,您有一个与之关联的ICommand

public class MyViewModel
{
    private ICommand _myCommand;
    public ICommand MyCommand
    {
        get
        {
            if (_myCommand== null)
            {
                _myCommand = new RelayCommand(
                    p => this.CanMyCommandExecute(),
                    p => this.MyCommandExecute()
            }
            return _myCommand;
        }
    }
}

You see that you create the ICommand instance in your view model and write the code for CanExecute method there too. 您会看到您在视图模型中创建了ICommand实例,并在那里也为CanExecute方法编写了代码。 The CanExecute method returns a bool which tells whether the command is able to execute under the current conditions. CanExecute方法返回一个布尔值,该布尔值指示命令是否能够在当前条件下执行。

WPF will automatically disable or enable the button connected with the ICommand depending on whether the CanExcute method returns true or false . WPF将根据CanExcute方法返回truefalse来自动禁用或启用与ICommand连接的按钮。

In the "CanExecute" method you can write code which takes the values of several other bound properties of your view model into account and then returns true of false. 在“ CanExecute”方法中,您可以编写代码,该代码将视图模型的其他几个绑定属性的值考虑在内,然后返回true或false。

So let's say you have 3 TextBox controls and 3 bound string values in your view model. 假设您的视图模型中有3个TextBox控件和3个绑定的字符串值。 In the CanExecute method you check the values of these 3 strings properties and return true or false. 在CanExecute方法中,检查这3个字符串属性的值,并返回true或false。 The button will then be enabled or disabled accordingly. 然后将相应地启用或禁用该按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM