wpf Prism Delegate 命令未启用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14843401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prism Delegate command not enabling button
提问by isakavis
I am trying to enable and disable a button in a WPF (PRISM) user control based on data entered by the user.
我正在尝试根据用户输入的数据启用和禁用 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();
}
}
}
I bound the SubmitCommand in the Xaml as below
我在 Xaml 中绑定了 SubmitCommand 如下
<Button Content="Submit" Width="100" Command="{Binding Path=SubmitCommand}" CommandParameter="{Binding ElementName=TitleText, Path=Text}" />
The issue is when title value changes, the button does not get enabled. May be I am missing something. Thanks for your help!
问题是当标题值更改时,按钮不会启用。可能是我错过了一些东西。谢谢你的帮助!
回答by NateTheGreat
It sounds like you're needing to raise the CanExecuteChangedevent on your command. For more details, see http://wpftutorial.net/DelegateCommand.htmland http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
听起来您需要CanExecuteChanged根据您的命令引发事件。有关更多详细信息,请参阅http://wpftutorial.net/DelegateCommand.html和 http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
Note that the first link is to an implementation of DelegateCommand, and is probably not what you're actually using. For the prism DelegateCommand, you simply need to call the RaiseCanExecuteChanged()method when you want to determine whether the button should be reenabled.
请注意,第一个链接是 DelegateCommand 的实现,可能不是您实际使用的链接。对于prism DelegateCommand,您只需RaiseCanExecuteChanged()在要确定是否应重新启用按钮时调用该方法。
Good luck!
祝你好运!
Nate
内特
回答by lloyd christmas
Add:
添加:
SubmitCommand.RaiseCanExecuteChanged();
After:
后:
this.RaisePropertyChanged();

