Closed richo-tri closed 4 years ago
Visibility="{Binding MenuItems, ElementName=MyWindow, Converter={StaticResource EmptyListVisibilityConverter}}"
Why do you think the EmptyListVisibilityConverter
should be called again? Thats not how WPF bindings work, you are binding against the list, the list does not get replaced, its still the same list.
Your converter code is accessing arbitrary properties off the C# objects, the binding engine can't know what properties you are depending on. What you instead want to do in this situation is create a MultiBinding with all dependencies and then use a converter which just combines the dependencies. Don't go collecting your dependencies manually in the converter code because then the binding engine doesn't know when you need to be updated.
Or rather, since you just have one dependency, you can do a direct binding against this dependency and run it through your converter, like you did in your other example that works.
Visibility="{Binding MenuItems.Count, ElementName=MyWindow, Converter={StaticResource ListCountVisibilityConverter}}"
Thats the correct way to do it, the binding engine now knows it has to notify the converter when Count
changes.
I assumed that the observable collection would work similar to how it does when bound to the ItemsControl. The ItemsControl updates just fine.
The ItemsControl manually subscribes itself to its ItemsSource, it doesn't get updated by the binding engine either. You could do the same in your converter, but its not going to happen automatically. How should the binding system know that your converter is accessing random properties on the object instead of just checking it for null and calculating the visibility from that? (If you try to subscribe yourself inside the converter you'll have a hard time getting the converter reevaluated though, thats not how the system is designed, better just tell the binding engine your dependencies and let it do its job.)
Binding to count property
Ok thanks for explaining.
Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No
Problem description: When binding an Observable collection to a control's visibility, it is not working/updating. Logging the converter shows that initially null comes in, then an empty list shows up. So the button is not displayed even though it has 2 items. It works fine for the other button which uses ObservableCollection.Count
Actual behavior: Visibility is not working probably because some update is not triggered.
Expected behavior: Visibility of button updates accordingly if items are added to the collection.
Minimal repro: https://github.com/richo-tri/WpfObservableCollectionVisibility The "This button is broken" button doesn't appear even though I believe it should