0

I have a button bound to a command, i want for it to be clickable 6 times at most and this works just fine, after the 6th time nothing happens

<Button Command="{Binding MyCommand}">Button Text</Button>

However i'm noticing a pretty major difference with what happened in WPF when i did the same thing. In WPF the command stops working after the 6th time AND the button becomes disabled, in UWP i can still keep hitting the button (it's enabled) but Nothing happen (the command doesn't get executed).

Am i doing Something wrong? Is IsEnabled not set automatically by the ICommand in UWP if it returns false to CanExecute?

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • That's a pretty big change but it's exactly it, do you want to post the answer or should i flag my question as duplicate? – Ronan Thibaudau Dec 01 '15 at 18:02
  • Please verify that when you raise `CanExecuteChanged` explicitly, the button's enabled state is correctly updated. If that turns out to not be the case, please post a new question but this time include a good [mcve] that reliably reproduces the problem. – Peter Duniho Dec 01 '15 at 18:30
  • Automatically raising CanExecuteChanged is actually an implementation of the Command... for example, I know MVVM Light's RelayCommand will automatically raise CanExecuteChanged anytime any property changes, while Microsoft PRISM's DeleteCommand does not (I prefer PRISM's Command, so encounter this a lot). A general solution is to add a PropertyChanged event handler to the object model, and if the property changing is one the Command uses to determine if it's valid or not, it calls `.RaiseCanExecuteChanged()` on the command – Rachel Dec 01 '15 at 18:43
  • @PeterDuniho It's what i meant by "that's exactly it" it did fix my issue just fine. – Ronan Thibaudau Dec 01 '15 at 18:44
  • @Rachel: _"Automatically raising CanExecuteChanged is actually an implementation of the Command"_ -- that's true, but most people used to WPF trying to figure out Winrt/UWP run into trouble because WPF doesn't rely on the `CanExecuteChanged`, instead opting to just call `CanExecute()` over and over. That's what the answer to the duplicate question explains. – Peter Duniho Dec 01 '15 at 18:48
  • @Peter Yes I thought that's what I said in my comment... depending on how the ICommand interface is implemented, the `RaiseCanExecuteChanged` may not be raised automatically whenever properties change. I actually don't like the sample code posted in the duplicate question because it basically re-introduces the exact same problem of raising CanExecuteChanged anytime any property changes, resulting in CanExecute getting queried far more than needed. It should instead have a check so RaiseCanExecuteChanged only executes if a property from CanExecute changes. – Rachel Dec 01 '15 at 19:10

0 Answers0