Binding ObserverableCollection to TextBlock does not trigger on updates #20099
-
|
I have an ObservableCollection data model, bound to an ItemsRepeater, and to a TextBlock, simplified as follows When I make changes to the collection, the repeater updates normally on each change, but the TextBlock binds once only, and then never updates to reflect the latest state of the collection. Is there a way to get updates to trigger on the TextBlock? Adding
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
That's to be expected. The Bindings don't bind to the contents of the collection, they bind to the collection itself. An ItemsRepeater knows what to do when its ItemsSource is given an ObservableCollection to bind to, but the IsVisible property on the TextBlock does not. As the ObservableCollection doesn't change, TextBlock never updates. One thing you could do is instead bind the TextBlock to MyCollection.Count |
Beta Was this translation helpful? Give feedback.
-
|
Interesting. In this case I'm passing it to a converter, but I guess the update context is still dictated by the control type. Also, I didn't realize you could chain sub properties on binding, that's really handy to know. Thanks for the answer. |
Beta Was this translation helpful? Give feedback.
That's to be expected. The Bindings don't bind to the contents of the collection, they bind to the collection itself. An ItemsRepeater knows what to do when its ItemsSource is given an ObservableCollection to bind to, but the IsVisible property on the TextBlock does not. As the ObservableCollection doesn't change, TextBlock never updates.
One thing you could do is instead bind the TextBlock to MyCollection.Count
If MyCollection is specifically presented as an ObservableCollection I believe they produce Property Changed notifications on the Count property, and so the Binding would update when the count changes.