CalcBinding icon indicating copy to clipboard operation
CalcBinding copied to clipboard

Consider lazy evaluation in conditional expressions

Open kunom opened this issue 4 years ago • 3 comments

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.)

kunom avatar Jun 17 '20 08:06 kunom

What is the MessagesFiltered ? A list or array ?

keytrap-x86 avatar Dec 13 '20 21:12 keytrap-x86

@varKeytrap MessagesFiltered is of type ObservableCollection<SomeInternalT>.

kunom avatar Dec 14 '20 07:12 kunom

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.

metal450 avatar May 31 '21 19:05 metal450