2

Basically I have the following situation:

<TextBox Text="{Binding MyIntValue}" />
<Button prism:Click.Command={Binding MyCommand}" />
public Boolean CanDoCommand()
{
    return (MyIntValue < 100);
}

public void DoCommand() { ... }

So here's the problem, if I type in the value of 25 the MyCommand becomes enabled. Afterwards, if I change it to 25A the Button is still enabled because the binding was not updated to reflect an error in my ViewModel. Instead, I only have an binding error on my View. This leaves the MyCommand button enabled and the MyIntValue still at 25.

How can I disable the button based on having any binding issues even if my ViewModel is proper?

Edit (What the poster is truly asking for):

How can I disable a button regardless of what the CanExecute method returns from the ViewModel based upon the View having a BindingError?

myermian
  • 31,823
  • 24
  • 123
  • 215
michael
  • 14,844
  • 28
  • 89
  • 177
  • Basically, to anyone wondering... the OP is asking: `How can I disable the button whether or not the CanExecute returns true/false?` That is, some logic that belongs on the VIEW LAYER ONLY (the answers should not involve changing his ViewModel). – myermian Jun 14 '11 at 14:55

3 Answers3

0
<Button prism:Click.Command={Binding MyCommand, 
    UpdateSourceTrigger=PropertyChanged}" /> 
Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
0

You must raise the command's can execute changed event when MyIntValue changes.

Damian Schenkelman
  • 3,505
  • 1
  • 15
  • 19
0

if your MyIntValue property is type of int your binding will never update when your input is 25A.

ony way to solve this is to use type of string and IDataErrorInfo on VM side.

another way is to use typeof Nullable int and a converter and set the value to null when its not what you expect.

EDIT:

How can I disable the button based on having any binding issues even if my ViewModel is proper?

your problem is that your VM and your UI is not in sync. if you type 25A your Vm seems right because it still has the 25, but your View has an BindingError. so your question should be how can i sync my view and viewmodel. (see my two suggestions)

EDIT: another solution would be to prevent wrong input. so a Masked or RegexTextbox behavior should also work.

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Forcing your ViewModel to account for possible errors on the View is not good if you're following MVVM. You're VM shouldn't have to change it's properties to String's and then do some hack conversion back into an Int just to appease ONE view. Sorry, but you're solution is a working solution that breaks the mvvm design pattern. Instead, the user really needs a way to keep the button disabled even if the ViewModel raises the command's CanExecute as true. – myermian Jun 14 '11 at 14:51
  • well i dont think so, the VM is for the View only and the VM should do what i need for my View. the model still has the int property, so all its fine. what i really dont like is when the view and the VM is not in sync. btw why this breaks the MVVM pattern? – blindmeis Jun 17 '11 at 18:00
  • Because you are suggesting to write logic in the ViewModel to account for a view. The viewmodel's job is not to worry about the view having a binding error or not. For example, if you want to change an item's Visibility you don't convert a boolean to a Visibility type on the ViewModel, you do so on the View. – myermian Jun 18 '11 at 00:58