OK, the XAML is quite simple and uses MVVM to bind to an ICommand SomeCommand { get; }
property on a view model:
My colleague found an elegant solution: using a binding fallback value!
public class NullCommand : ICommand
{
private static readonly Lazy _instance = new Lazy(() => 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 then the XAML looks like:
The advantage of this solution is that it works better if you break Law of Demeter and you have some dots in the binding path, where each instance might become null
.