0

I have a page with two buttons (I really have ten, but for example sake, lets say two), and they are both bound to a RelayCommand and each has a CommandParameter bound to two different columns from the database. Something like this,

<Button  Command="{Binding DataContext.CopyPasteDataValueCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                         CommandParameter="{Binding AccountID}">
<Button  Command="{Binding DataContext.CopyPasteDataValueCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                         CommandParameter="{Binding CustomerID}">

I have the associated RelayCommands setup properly in the code behind, and everything works just fine. Except when I select a different row in my DataGrid and I want to force rebinding and recalling of the RaiseCanExecute of both those buttons.

I would like to make something like this

private void RaiseAllCanExecuteChanged(){
CopyPasteDataValueCommand.RaiseCanExecuteChanged<string>("AccountID");
CopyPasteDataValueCommand.RaiseCanExecuteChanged<string>("CustomerID");
}

Of course this won't compile. But I'm wondering how would I do this, or if I indeed, CAN do this. Is this possible or do I really need to have multiple RelayCommands for each occurrence?

Thank you very much.

PHenry
  • 287
  • 3
  • 16

2 Answers2

1

Interesting, for tricks, I tried just firing the

CopyPasteDataValueCommand.RaiseCanExecuteChanged();

and put a breakpoint in the CanExecute method.....and low and behold!!!! it fired it for each time I wanted it to fire, passing in the CommandParameter! WOW! Super!

So, it looks like there is some type of internal array\list that's tracking the CommandParameters and firing the ONE RaiseCanExcecuteChanged method.....fires ALL the other associated ones for me. Sweet!

PHenry
  • 287
  • 3
  • 16
-1

Implement an event handler for the SelectionChanged event on your DataGrid.

Within the event handler invoke your viewmodel's logic for the following:

CopyPasteDataValueCommand.RaiseCanExecuteChanged<string>("AccountID");
CopyPasteDataValueCommand.RaiseCanExecuteChanged<string>("CustomerID");
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • Thank you Scott for the reply. I did do the event handler, unfortunately that code you have there, is a copy\paste from my code above, and unfortunately it doesn't compile as the RaiseCanExecuteChanged is currently an empty argument method, and not a generic one neither. But I was able to find a solution, much simpler than I had originally thought. I was over thinking things, I just need to call the RaiseCanExecuteChanged...and the system will take care of the rest (ie calling the individual calls) for me. – PHenry Apr 21 '15 at 19:14