c# - 使用 WPF 验证命令

标签 c# wpf entity-framework data-binding command

我是 WPF 和 MVVM 的新手,所以请耐心等待。

基本上,我正在创建一个应用程序,使用户能够将他们的组织详细信息输入到数据库中。

在我正在创建的 WPF 应用程序中,我有一个 View Model,其中包含 propertiescommandsentity framework 方法(我知道这不是使用 MVVM 的正确方法,但我正在慢慢学习如何实现它)。

在我的一个 View 中,它是一个选项卡控件,允许用户将不同的组织详细信息输入数据库,然后在另一个 View 中,我有一个数据网格来显示他们输入的内容,使用户能够更新需要时的内容。

这引出了我的问题。到目前为止,我已经验证了我的命令,以便当某些字段为空时,按钮将不会激活,但一旦输入它们,它们就会被激活。像这样;

           private ICommand showAddCommand;
    public ICommand ShowAddCommand
    {
        get
        {
            if (this.showAddCommand == null)
            {
                this.showAddCommand = new RelayCommand(this.SaveFormExecute, this.SaveFormCanExecute);//i => this.InsertOrganisation()
            }

            return this.showAddCommand;
        }
    }

    private bool SaveFormCanExecute()
    {
        return !string.IsNullOrEmpty(OrganisationName) && !string.IsNullOrEmpty(Address) && !string.IsNullOrEmpty(Country) && !string.IsNullOrEmpty(Postcode)
            && !string.IsNullOrEmpty(PhoneNumber) && !string.IsNullOrEmpty(MobileNumber) && !string.IsNullOrEmpty(PracticeStructure) && !string.IsNullOrEmpty(PracticeType) && !string.IsNullOrEmpty(RegistrationNumber); 
    }

    private void SaveFormExecute()
    {
        InsertOrganisations();
    }

  Xaml:
  <Button Content="Save" Grid.Column="1" Grid.Row="18" x:Name="btnSave" VerticalAlignment="Bottom" Width="75" Command="{Binding ShowAddCommand}"/>

但我希望实现的是,一旦用户将 1 个组织输入到数据库中,该命令就不会完全激活,并防止用户意外进入另一个组织。目的是只允许添加1个组织,不多也不少。

这可能吗?

最佳答案

两件事。 1),我建议编辑您的 RelayCommand 以执行如下操作:

    private Action<object> _execute;
    private Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    //[DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

这可能不会解决您的直接问题,但它会使您的代码更可重用,因为您将能够将 View 中的某些元素绑定(bind)为 ICommand 中的参数以进行 CanExecute 测试和执行(或者如果您不会不需要这个,只需将 null 作为 ICommand.Execute 中的对象参数传递,并且不要将任何内容绑定(bind)到 View 中的 CommandParameter)

此外,在您的 RelayCommand 中,确保您按如下方式覆盖 CanExecuteChanged:

    public override event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

这将触发您的 ICommand 在用户进行更改时重新检查 CanExecute(这很可能是您遗漏的部分)。

最后,完成此操作后,只需将您的条件包含在 CanExecute 中即可(来自 CommandParameter 或来自您 VM 中的某些内容)。

希望对您有所帮助。

关于c# - 使用 WPF 验证命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13685124/

相关文章:

c# - 即使属性存在,编译时也会出现 CS1061 错误

c# - 使用 AsParallel

c# - 在wpf中动态添加列到DataGrid

c# - 列名称无效。该名称从未在代码中实现

c# - 需要澄清 : abstract classes preferred over interfaces. 原因:他们将契约(Contract)与实现分离

c# - 串口软件占用和硬件占用的区别

wpf - 使用 BlockUIContainer 进行流文档分页

c# - 将 DbContext 与依赖注入(inject)一起使用

sql-server - Insert 语句与 FOREIGN KEY 约束冲突。 Entity Framework

c# - 在非标识主键中插入一个值