XAML Command aspect in user controls

henry.nordstrom's Avatar

henry.nordstrom

23 Jul, 2017 12:27 PM

I tried to use the XAML Command aspect inside the datacontext of a user control as follows:

[Command]
public ICommand UseSelectedAddressCommand { get; private set; }

[SafeForDependencyAnalysis]
public bool CanExecuteUseSelectedAddressCommand => MySelectedAddress != null;

private void ExecuteUseSelectedAddressCommand()
{
      Mapper.Map(MySelectedAddress, MyAddress);
}

This worked perfectly on a normal XAML form, but not within a user control.
However, I was able to fix this by explicitly specifying the ExecuteMethod and CanExecuteProperty:

[Command (ExecuteMethod = "ExecuteUseSelectedAddressCommand", CanExecuteProperty = "CanExecuteUseSelectedAddressCommand")]

I don't know why this happens, but at least it works now. Anyway, maybe this could be improved in some future release of PostSharp.

  1. Support Staff 1 Posted by PostSharp Techn... on 24 Jul, 2017 01:13 PM

    PostSharp Technologies's Avatar

    Hello,

    I've tested this both on a Window and a UserControl and I wasn't able to reproduce the issue. Are you using the latest PostSharp version 5.0.29?

    I've also noticed that you're not following the naming conventions correctly. In your case, the method names should be UseSelectedAddressCommand, CanExecuteUseSelectedAddress, and ExecuteUseSelectedAddress as described at http://doc.postsharp.net/command.

    If this renaming does not solve your issue, could you please provide us a (minimal) example which reproduces the issue?

    Best regards,
    -tony

  2. henry.nordstrom closed this discussion on 01 Aug, 2017 02:58 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac