0

Is there any way to get rid of the ArgumentOutOfRangeException messages in the output window when binding to an array element that might not be set?

For a MenuItem, here is my binding:

            <Setter Property="InputGestureText" Value="{Binding Path=MenuItem.Command.InputGestures[0].DisplayString}"/>

If InputGestures is of 0 length, shouldn't this just set the binding value to null? Do I now have to make a custom converter just so I can first check if Count>0? I thought the binding did that automatically... Thanks for any info!

DanW
  • 1,976
  • 18
  • 14
  • Is your binding to a C# object? If so, you do not need a full blown converter but could get away with something like `public string FirstInputGesture => MenuItem.Command.InputGestures.Any() ? MenuItem.Command.InputGestures[0].DisplayString : null`. – fuglede Mar 27 '17 at 18:49
  • It's in a container style for a Menu. The items are IMenuItem and its Command is an ICommand. The underlying commands don't necessarily even have InputGestures. RoutedCommands do, but others might not. If they did, the binding should find it. Don't "collection[0]" type bindings return null if the collection is empty in the first place? I'd hate to have to implement some reflection converter just to avoid output window messages - at best that would be really slow. – DanW Mar 28 '17 at 15:08
  • Not sure if this can help: http://stackoverflow.com/questions/1895453/set-value-to-null-in-wpf-binding – Scott Nimrod Mar 28 '17 at 16:46
  • I don't think so - the object is set but empty, or it doesn't exist, but isn't null. – DanW Mar 29 '17 at 17:44

0 Answers0