I have a TextBox that is tied to a command like this:
<TextBox Text="{Binding Path=TextContent, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=MyCommand}" Key="Enter" />
</TextBox.InputBindings>
</TextBox>
The property TextContent
is a string defined in the ViewModel. The command MyCommand
is also defined in the ViewModel. The ViewModel does not know the View.
The command will be called whenever the TextBox has focus and the enter key is hit. Unfortunately, if CanExecute
returns false, the user cannot see (visually) that the command was not executed, because there is no visual change in the TextBox.
I am looking for advice on how to show the user that the command could not be executed after he had pressed enter.
My ideas (and my doubts about them):
Disabling the TextBox when
CanExecute
returnsfalse
: This is no option because the return value ofCanExecute
can change everytime a letter is typed/changed (the text in the TextBox influences the outcome ofCanExecute
). When it is disabled for the first time, the user cannot type into it any more, so it will stay disabled forever.Show a message box saying that the command was not executed: Remember, the ViewModel does not know the View. Is it even possible to open a message box from the ViewModel? Furthermore, where should I put the call to opening a message box? Not inside
CanExecute
because I only want to get the message box after hitting enter, not everytimeCanExecute
returnsfalse
. Maybe makeCanExecute
always returntrue
and do the checks insideExecute
: If checks are okay, do the command stuff, if not, show some message to the user. But then, the point of havingCanExecute
is missed entirely...
I want to keep MVVM, but some codebehind for redirecting stuff to the ViewModel seems okay for me.