Reputation: 785
I am trying to enable and disable a button in a WPF (PRISM) user control based on data entered by the user.
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
<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!
Upvotes: 4
Views: 6535
Reputation: 576
Add:
SubmitCommand.RaiseCanExecuteChanged();
After:
this.RaisePropertyChanged();
Upvotes: 0
Reputation: 2305
It sounds like you're needing to raise the CanExecuteChanged
event on your command. For more details, see http://wpftutorial.net/DelegateCommand.html and 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.
Good luck!
Nate
Upvotes: 5