[BUG] MAUI Toolkit Converters Not Triggering for ObservableCollection Property changes. #2473
-
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current BehaviorI have created a custom control using the CollectionView that includes a floating "Add" button. The visibility of this button depends on two conditions:
To implement this, I used the following .NET MAUI Toolkit converters:
The Button visibility is bound using a MultiBinding as shown below: <Button
Grid.Row="0"
Text="Add"
-----
VerticalOptions="End">
<Button.IsVisible>
<MultiBinding Converter="{StaticResource IsMultiValueAllTrueConverter}">
<Binding
Path="Attachments"
Converter="{StaticResource IsListNotNullOrNotEmptyConverter}"/>
<Binding Path="IsEditable"/>
</MultiBinding>
</Button.IsVisible>
</Button> Issue:When deleting all items from the AttachmentList, the converters are not triggered, and the floating "Add" button remains visible, even though the list is empty. Despite implementing INotifyPropertyChanged for AttachmentList manually, the behavior persists. Workaround:To resolve the issue temporarily, I introduced a new bindable property, HasData, which is a boolean. Its value is updated during data loading and deletion actions. This property is then used in the MultiBinding, and the visibility logic works as expected: <Button
Grid.Row="0"
Text="Add"
-----
VerticalOptions="End">
<Button.IsVisible>
<MultiBinding Converter="{StaticResource IsMultiValueAllTrueConverter}">
<Binding Path="HasData"/>
<Binding Path="IsEditable"/>
</MultiBinding>
</Button.IsVisible>
</Button> Expected BehaviorThe IsMultiValueAllTrueConverter and IsListNotNullOrNotEmptyConverter must handle changes in any ObservableCollection accurately. Specifically, they should respond to Add and Delete operations within the collection. Steps To Reproduce
The.issue.movLink to public reproduction project repositoryhttps://github.com/divyesh008/Converters_bug.git Environment- .NET MAUI CommunityToolkit: 9.0.3, 11.0.0
- OS: Macos 16.0, Windows Build 10.0.19041.0
- .NET MAUI: 9.0.0/9.0.100 Anything else?Request:Please investigate this and provide a fix or guidance for a more robust solution. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Be sure to manually assign More information is available in the release notes: https://github.com/CommunityToolkit/Maui/releases/tag/10.0.0 Additional information available on StackOverflow: https://stackoverflow.com/a/79099574/5953643 |
Beta Was this translation helpful? Give feedback.
-
@brminnick The issue is not related to behavior but rather to the ObservableCollection property changes not being tracked by the .NET MAUI Toolkit converter. In other words, the changes in the ObservableCollection are not being properly notified or observed by the converter. |
Beta Was this translation helpful? Give feedback.
-
@divyesh008 the converter should not subscribe to any events on the ObservableCollection in order to monitor changes inside the collection because there is no lifecycle to know when to unsubscribe from the events. If you want to achieve something like this I would recommend either writing the code behind in your control or creating something like a Behavior to solve your issue. |
Beta Was this translation helpful? Give feedback.
-
I think there's a typo and the list converter is named IsListNotNullOrEmptyConverter and not Your problem could be solved if you were to bind to As an example of this, I use the MultiMathExpressionConverter with an appropriate expression. <Button
Grid.Row="0"
IsVisible="{MultiBinding {Binding Attachments.Count},
{Binding IsEditable},
Converter={StaticResource MultiMathExpressionConverter},
ConverterParameter='!! x0 and x1'}"
Text="Add"
VerticalOptions="End" /> |
Beta Was this translation helpful? Give feedback.
-
I'm able to handle this using <Button.IsVisible>
<MultiBinding Converter="{StaticResource IsMultiValueAllTrueConverter}">
<Binding Path="Attachments.Count" Converter="{StaticResource IntToBoolConverter}"/>
<Binding Path="IsEditable" />
</MultiBinding>
</Button.IsVisible> |
Beta Was this translation helpful? Give feedback.
I'm able to handle this using