开发者

Invalid data in TextBox -- How to disable my command buttons?

开发者 https://www.devze.com 2023-03-13 03:28 出处:网络
Basically I have the following situation: <TextBox Text=\"{Binding MyIntValue}\" /> <Button prism:Click.Command={Binding MyCommand}\" />

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?


<Button prism:Click.Command={Binding MyCommand, 
    UpdateSourceTrigger=PropertyChanged}" /> 


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号