ZGholson
Topic Author
Posts: 9
Joined: 04 Aug 2022, 19:12

Converter lagging behind updates

19 Oct 2022, 22:13

Hello!

I am filtering through an observable collection and counting element with a specific property and returning a string of that value. Upon using this converter, the value seems lag behind an update. For instance, if I increase the count, it shows the previous count. However, the initial count on load seems to be correct. On the other hand, not using the converter seems to update correctly.

Is there a chance that I may be missing something to force the value to update with the converter?
ListCountConverter::TryConvert(
    Noesis::BaseComponent* itemsSource,
    const Noesis::Type* /* targetType */,
    Noesis::BaseComponent* parameter,
    Noesis::Ptr<Noesis::BaseComponent>& result)
{
    int itemCount = 0;
    Noesis::IList* items = Noesis::DynamicCast<Noesis::IList*>(itemsSource);
    Noesis::ObservableCollection<CustomStruct>* structs =
        Noesis::DynamicCast<Noesis::ObservableCollection<CustomStruct>*>(
            items);

    if (items && Noesis::Boxing::CanUnbox<Noesis::String>(parameter))
    {
        const char* filterString = Noesis::Boxing::Unbox<Noesis::String>(parameter).Str();

        int numItems = structs->Count();
        for (int i = 0; i < numItems; ++i)
        {
            CustomStruct* structItem = structs->Get(i);
            if (structItem->GetSubscription() == filterString)
            {
                itemCount++;
            }
        }
    }
    else
    {
        return false;
    }
    std::string returnValue = itemCount > 0 ? "(" + std::to_string(itemCount) + ")" : "";
    result.Reset(Noesis::Boxing::Box(returnValue.c_str()));
    return true;
}
<TextBlock
          VerticalAlignment="Center"
          Foreground="{DynamicResource ColorBrush.white}"
          Text="{Binding Path=itemList, Converter={StaticResource ListCountConverter}, ConverterParameter=test, UpdateSourceTrigger=PropertyChanged}" />
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Converter lagging behind updates

20 Oct 2022, 14:09

Hi, one thing you have to consider is that adding/removing items from the "itemList" collection won't trigger a property change (the collection instance set in the property is the same) and the binding won't be reevaluated, so the converter won't be called. You need manually notify in your view model a PropertyChanged for the "itemList" property if you want the binding to call the converter and update the TextBlock.

I created a similar test and calling PropertyChanged whenever I do changes to the collection, then the TextBlock always shows the correct count. Could you try that?

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 34 guests