For WPF ICommand, what is the equivalent of the event handler -=?
We have a user control button that manages the function of submitting a purchase request but requires a user to register on the workstation first.
The WinForms usage is:
this.Click -= this.btnRegister_Click;
this.Click -= this.btnSubmit_Click;
A modernized version in WPF has a single ICommand property and the typical declaration.
public ICommand ClickCommand { get; set; }
....
ClickCommand = new DelegateCommand(RegisterClicked);
Once the user registers, the user control will reinitialize to the submit process. A simple trigger exists that checks the status of the button. Visually, the triggers are working just fine which indicates that the program is processing the initialization code blocks.
<Label.Style>
<Style TargetType="Label" BasedOn="{StaticResource ButtonLabel}">
<Setter Property="Content" Value="Approvals"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ButtonState}" Value="{x:Static locdata:WorkflowButtonState.Register}">
<Setter Property="Content" Value="Register"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ButtonState}" Value="{x:Static locdata:WorkflowButtonState.Submit}">
<Setter Property="Content" Value="Submit"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
Initialization code blocks
private void InitRegister()
{
ButtonState = WorkflowButtonState.Register;
ClickCommand = new DelegateCommand(RegisterClicked);
}
private void InitSubmit()
{
ButtonState = WorkflowButtonState.Submit;
ClickCommand = new DelegateCommand(SubmitClicked);
}
private void Reset()
{
ClickCommand = null;
ButtonState = WorkflowButtonState.Default;
}
Debug mode clearly shows that the ClickCommand execute method is the SubmitClicked, however the RegisterClicked event still fires. This only happens when the Register process occurs first because the user has not previously logged on the workstation.
What is needed to replicate the EventHandler -= behavior since setting ClickCommand = null did not help?
UPDATE
The ICommand usage in my applications to this point has been a single use. As Andy's answer stated, it should be treated like a property and to raise the property changed event.
The updated ICommand declaration was, as I have done numerous times for properties, all that needed changed.
private ICommand _clickCommand;
public ICommand ClickCommand { get => _clickCommand; set { _clickCommand = value; OnPropertyChanged(); } }