启用按钮和 DelegateCommand

发布于 2024-08-07 17:16:05 字数 587 浏览 2 评论 0原文

TextBox 中输入所需数据之前,如何禁用 Button

我将 Button 绑定到 ICommand

public ICommand LoginCommand
{
    get
    {
        if (_loginCommand == null)
        {
            _loginCommand = new DelegateCommand<string>(this.Login, this.IsValid);
        }
        return _loginCommand;
    }
}

在 XAML 中,如下所示:

<Button Style="{StaticResource LoginButton}" Content="Login" Command="{Binding LoginCommand}" CommandParameter="{Binding Text, ElementName=txtUserName}" />

How can I disable a Button until the required data is entered in a TextBox?

I'm binding a Button to an ICommand:

public ICommand LoginCommand
{
    get
    {
        if (_loginCommand == null)
        {
            _loginCommand = new DelegateCommand<string>(this.Login, this.IsValid);
        }
        return _loginCommand;
    }
}

in XAML like this:

<Button Style="{StaticResource LoginButton}" Content="Login" Command="{Binding LoginCommand}" CommandParameter="{Binding Text, ElementName=txtUserName}" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-08-14 17:16:05

您只需要引发重新查询事件。使用 DelegateCommand 这很容易。这是您的 ViewModel 的外观。

另外,如果没有令人信服的理由将您的属性设置为 DelegateCommand 类型,那么您应该这样做。

public class MyViewModel : ViewModel
{

    private string _myTextField;
    public string MyTextField
    {
        get { return _myTextField; }
        set
        {

            _myTextField = value;
            OnPropertyChanged("MyTextField");

            //Here's the magic
            LoginCommand.RaiseCanExecuteChanged();

        }
    }

    public DelegateCommand<string> LoginCommand { get; set; }

    public MyViewModel()
    {
         LoginCommand = new DelegateCommand<string>(Login, CanLogin);
    }

    public bool CanLogin(string text)
    {
         return !string.IsNullOrEmpty(text);
    }

    public void Login(string text)
    {
         //login logic
    }

}

差不多就这样了。

You just need to raise the requery event. This is easy with a DelegateCommand. Here's how your ViewModel would look.

Also, if there's not a compelling reason to make your property of type DelegateCommand, then you should do so.

public class MyViewModel : ViewModel
{

    private string _myTextField;
    public string MyTextField
    {
        get { return _myTextField; }
        set
        {

            _myTextField = value;
            OnPropertyChanged("MyTextField");

            //Here's the magic
            LoginCommand.RaiseCanExecuteChanged();

        }
    }

    public DelegateCommand<string> LoginCommand { get; set; }

    public MyViewModel()
    {
         LoginCommand = new DelegateCommand<string>(Login, CanLogin);
    }

    public bool CanLogin(string text)
    {
         return !string.IsNullOrEmpty(text);
    }

    public void Login(string text)
    {
         //login logic
    }

}

That's pretty much it.

萌酱 2024-08-14 17:16:05

DelegateCommand 在哪里定义的?检查构造函数,它可能期望第二个参数是谓词,其结果被映射到您的 ICommandCanExecute

我假设 this.IsValid 是一个委托方法,如果 string.Length > 则返回 true。 0 表示您从视图传递的命令参数...尝试将其设置为始终返回 false 只是为了检查 UI 是否禁用该按钮。

编辑:
要将参数获取到 IsValid 方法,请使用以下语法:

更改 IsValid 方法的语法以接受字符串参数,然后像这样 decalre 您的 DelegateCommand:

_loginCommand = new DelegateCommand<string>(this.Login, (param) => this.IsValid(param));

HTH :)

Where is DelegateCommand defined? Check the constructor, it is probably expecting the second parameter to be a predicate, the result of which gets mapped to your ICommand's CanExecute.

I assume that this.IsValid is a delegate method that returns true if the string.Length > 0 for the Command parameter you are passing from the View... Try setting it to always return false just to check the UI is disabling the button.

EDIT:
To get the param into the IsValid method, use the following syntax:

change the syntax of the IsValid method to accept a string parameter, then decalre your DelegateCommand like:

_loginCommand = new DelegateCommand<string>(this.Login, (param) => this.IsValid(param));

HTH :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文