View Issue Details

IDProjectCategoryView StatusLast Update
0001658NoesisGUIC# SDKpublic2020-04-16 15:46
Reporterai_enabled Assigned Tosfernandez  
PrioritynormalSeverityminor 
Status resolvedResolutionno change required 
Product Version3.0.0 
Target Version3.0.0Fixed in Version3.0.0 
Summary0001658: Binding to ObservableCollection doesn't trigger change event anymore
Description

Hi guys,

I'm not sure whether it's intended but in 2.2.6 it works fine while in 3.0.0b7 it's broken (and we've missed it during early testing).

For example:
<Grid Visibility="{Binding AccessibleVehicles, Converter={StaticResource EmptyCollectionToVisibilityConverter}}">
...

Where AccessibleVehicles is a readonly property of ObservableCollection<> type.

I was able to workaround it by adding NotifyPropertyChanged(nameof(AccessibleVehicles)) but it's really weird as in 2.2.6 it works just fine!

We have several such cases in the code.

Regards!

PlatformAny

Activities

ai_enabled

ai_enabled

2020-04-15 04:49

updater   ~0006208

Last edited: 2020-04-15 04:49

Here is the code of the converter:


public class EmptyCollectionToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// ReSharper disable once PossibleNullReferenceException
var valueAsEnumerable = value as IEnumerable;

    var hasElements = valueAsEnumerable?.GetEnumerator().MoveNext() ?? false;
    // ReSharper disable once CanExtractXamlLocalizableStringCSharp
    if (parameter as string == "Reverse")
    {
        hasElements = !hasElements;
    }

    return hasElements ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotImplementedException();
}

}

sfernandez

sfernandez

2020-04-16 09:56

manager   ~0006214

I've checked with WPF and the behavior seems correct.
If the collection changes (items added or removed) but the property itself didn't change (the collection is the same), the binding is not evaluated and the converter is not called.

You probably should use collection Count as the trigger of property change:
<Grid Visibility="{Binding AccessibleVehicles.Count, Converter={StaticResource CountToVisibilityConverter}}">

And use a converter that checks the value of the Count to determine the visibility.

ai_enabled

ai_enabled

2020-04-16 15:43

updater   ~0006216

Thank you for investigating! The advice with binding to Count is a golden one :-) I never thought about this!
I'm surprised it was fine before so I assumed it's a bug. Good to know that now it works as intended.
You can close this issue.

sfernandez

sfernandez

2020-04-16 15:46

manager   ~0006217

You're welcome.

Issue History

Date Modified Username Field Change
2020-04-15 04:48 ai_enabled New Issue
2020-04-15 04:49 ai_enabled Note Added: 0006208
2020-04-15 04:49 ai_enabled Note Edited: 0006208
2020-04-16 09:56 sfernandez Assigned To => sfernandez
2020-04-16 09:56 sfernandez Status new => feedback
2020-04-16 09:56 sfernandez Target Version => 3.0.0
2020-04-16 09:56 sfernandez Note Added: 0006214
2020-04-16 15:43 ai_enabled Note Added: 0006216
2020-04-16 15:43 ai_enabled Status feedback => assigned
2020-04-16 15:46 sfernandez Status assigned => resolved
2020-04-16 15:46 sfernandez Resolution open => no change required
2020-04-16 15:46 sfernandez Fixed in Version => 3.0.0
2020-04-16 15:46 sfernandez Note Added: 0006217