【问题标题】:Prism Delegate command not enabling buttonPrism Delegate 命令未启用按钮
【发布时间】:2013-02-12 22:54:38
【问题描述】:

我正在尝试根据用户输入的数据启用和禁用 WPF (PRISM) 用户控件中的按钮。

In the constructor I do
SubmitCommand = new DelegateCommand<object>(OnSubmit, CanSubmit);


public ICommand SubmitCommand { get; private set; }

private void OnSubmit(object arg)   
{
     _logger.Log(arg.ToString());
}

private bool CanSubmit(object arg) 
{
    return Title.Length > 0; 
}

private string _title="";

public string Title
{
get { return _title; }
set 
{
    if (_title != value)
    {
           _title = value;
       this.RaisePropertyChanged();
    }
}
}

我在 Xaml 中绑定了 SubmitCommand,如下所示

<Button Content="Submit" Width="100" Command="{Binding Path=SubmitCommand}" CommandParameter="{Binding ElementName=TitleText, Path=Text}" />

问题是当标题值改变时,按钮没有被启用。可能是我错过了一些东西。谢谢你的帮助!

【问题讨论】:

    标签: wpf prism


    【解决方案1】:

    听起来您需要在命令中引发CanExecuteChanged 事件。更多详情见http://wpftutorial.net/DelegateCommand.htmlhttp://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx

    请注意,第一个链接是 DelegateCommand 的实现,可能不是您实际使用的。对于 prism DelegateCommand,您只需在要确定是否应重新启用按钮时调用RaiseCanExecuteChanged() 方法即可。

    祝你好运!

    内特

    【讨论】:

    • 谢谢内特。正如你所说,我只需要调用 RaiseCanExecuteChanged。
    【解决方案2】:

    添加:

     SubmitCommand.RaiseCanExecuteChanged();
    

    之后:

     this.RaisePropertyChanged();
    

    【讨论】:

    • 感谢劳埃德的帮助。我不能直接使用 SubmitCommand 来引发,因为上面的 SubmitCommand 是 ICommand。我修改了代码以使用 .RaiseCanExecuteChanged(); 执行您所说的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多