如何禁用按钮,直到动态生成的所有条目或数据网格中的所有条目均已填充

时间:2019-09-09 09:59:04

标签: c# xaml xamarin.forms

我有一个表单,该表单具有动态生成的字段,我想禁用在填写所有条目后需要单击的按钮。 我正在使用MVVM体系结构。而且我的按钮正在使用AddScoreCommand。

    public SubmitDataViewModel()
    {
        AddScoreCommand = new Command(CmdSubData);

    }



    private async void CmdSubData()
    {
      //Scorelist contains data that is displayed in the form that needs 
       //to be submitted
        foreach (var element in Scorelist)
            {

            var _scoreDef = new scores
            {
                ID = element.ID,
                SCORES = element.SCORES, //Gets entered value from entry
                GOAL_ID = element.ID,


            };

           response = await apiServices.SubmitScore(_scoreDef); 

            }


    }

2 个答案:

答案 0 :(得分:1)

您可以使用数据绑定来绑定ViewModel中Button的属性 isEnable

xaml

<Button Command="{Binding AddScoreCommand}" IsEnabled="{Binding isEnable}"/>

在ViewModel中

public class SubmitDataViewModel:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ICommand AddScoreCommand { get; set; }


    private bool isenable;

    public bool isEnable
    {
        get
        {
            return isenable;
        }

        set
        {
            if (isenable != value)
            {
                isenable = value;
                NotifyPropertyChanged();
            }
        }

    }


    public SubmitDataViewModel()
    {
        //...

        isEnable = true; // set the default value
    }

    private async void CmdSubData()
    {
        //Scorelist contains data that is displayed in the form that needs 
        //to be submitted
        foreach (var element in Scorelist)
        {

            var _scoreDef = new scores
            {
                ID = element.ID,
                SCORES = element.SCORES, //Gets entered value from entry
                GOAL_ID = element.ID,


            };

            response = await apiServices.SubmitScore(_scoreDef);

        }

        isEnable = false; // change the value after submit 
    }

}

答案 1 :(得分:-1)

您可以通过jquery验证插件来实现。 (https://jqueryvalidation.org/

在提交控制器中,您还需要检查所有字段是否都不为空,以防止用户操作dom时出现问题。 (用户可以通过开发人员的工具轻松启用该按钮)