Alex141 / CalcBinding

Advanced WPF Binding which supports expressions in Path property and other features
Apache License 2.0
625 stars 78 forks source link

Consider lazy evaluation in conditional expressions #63

Open kunom opened 4 years ago

kunom commented 4 years ago

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

keytrap-x86 commented 3 years ago

What is the MessagesFiltered ? A list or array ?

kunom commented 3 years ago

@varKeytrap MessagesFiltered is of type ObservableCollection<SomeInternalT>.

metal450 commented 3 years ago

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.