CalcBinding
CalcBinding copied to clipboard
Consider lazy evaluation in conditional expressions
Consider the following binding expression: {calc:Binding 'MessagesFiltered.Count > 0 ? MessagesFiltered[0] : null'}
When MessagesFiltered
is empty, I see in the binding log the message below, which is exactly what the above binding expression tried to avoid.
System.Windows.Data Warning: 17 : Cannot get 'Item[]' value (type 'LogMessageWithSource') from 'MessagesFiltered' (type 'ObservableCollection`1'). BindingExpression:Path=MessagesFiltered[0]; DataItem='LogMessagesViewModel' (HashCode=50930930); target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index'
Is there the possibility to introduce lazy evaluation in such circumstances, or at least improve the error handling?
(Note: I also tried using a converter calling Enumerable.FirstOrDefault()
, but then you lose the change tracking.)
What is the MessagesFiltered
? A list or array ?
@varKeytrap MessagesFiltered
is of type ObservableCollection<SomeInternalT>
.
I'm having the same issue. Can't seem to figure out how to look at a property in an array, without it throwing up errors. {c:Binding 'Items.Count > 0 and !Items[0].SomeBool'}
behaves the same: it's functional, but it prints an ArgumentOutOfRangeException.