FallbackValue for Command
When you try to connect an MVVM command, it must be better to have a fallbackvalue if binding not release
a null static command return false is the solution for showed that the binding cannot be set (and not allow command)
for sample if i have the command : CloseCommand in Selected in my MVVM
when selected is set to null the binding not released
the fallbackvalue is set in this case
I use a static command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class NullCommand : ICommand { private static readonly Lazy<NullCommand> _instance = new Lazy<NullCommand>(() => new NullCommand()); private NullCommand() { } public event EventHandler CanExecuteChanged; public static ICommand Instance { get { return _instance.Value; } } public void Execute(object parameter) { throw new InvalidOperationException("NullCommand cannot be executed"); } public bool CanExecute(object parameter) { return false; } } |
and i use like this
1 2 |
<MenuItem Header="sample" Command="{Binding Selected.CloseCommand, FallbackValue={x:Static NullCommand.Instance}}"/> |
Categories: Non classé
Commentaires récents