Closed chenjing1294 closed 2 weeks ago
Should you add a converter in multibinding?
At a guess a MultiBinding does not produce a ReadOnlyCollection
@rabbitism @IanRawley If you don't add a Converter, the ReadOnlyCollection is passed when the CanCreateSlave method is called. The problem is that in 11.1.4, when I select different TreeItems, the CanCreateSlave method is called, but in 11.2.0, the CanCreateSlave method is never called.
You need to annotate your CanExecute equivalent method with the property or properties that can trigger changes. See: https://docs.avaloniaui.net/docs/guides/data-binding/how-to-bind-to-a-command-without-reactiveui#trigger-can-execute
It seems MultiBindings don't automatically trigger re-evaluations of CanExecute for method bound commands. Binding to a single Property does though. So the problem isn't TreeView, it's MultiBinding.
I now understand that it is not a problem with TreeView, but a problem with MultiBinding or Command. Even if I don't use annotation, the Can method should be triggered because the properties have changed.
I've figured out the problem here. When a MultiBinding doesn't have a converter, the associated MultiBindingExpressions simply publishes a wrapper ReadOnlyCollection around its internal values array. The collection itself isn't Observable, and never actually itself changes, so while the values contained have changed the list itself is the same. As a result, the Binding system just silently ignores it because from its point of view nothing has changed.
This was introduced by #16219 Previously a MultiBinding would instantiate a new collection of values every time it needed to publish a value without a converter. The new MultiBindingExpression instead reuses the same ReadOnlyCollection, and so will effectively only ever publish a value once.
@IanRawley I understand. Thank you very much for your answer.
Describe the bug
When I upgraded from 11.1.4 to 11.2.0, the command binding on the button failed. The following sample code can reproduce the problem.
11.1.4 good
https://github.com/user-attachments/assets/2a734bd3-f9a1-436b-b817-62a3cccd9b02
11.2.0 Have a problem
When "a" is selected, the button is still unavailable
https://github.com/user-attachments/assets/c2ae6499-7199-4672-87e6-ff1a76c541e1
To Reproduce
The following sample code can reproduce the problem.
Expected behavior
No response
Avalonia version
11.2.0
OS
No response
Additional context
No response