Reputation: 21
I have a ListBox in an Avalonia project that is bound to an ObeservableCollection<MyType>
which is grabbed from an EF Core request _myContext.MyTypes.ToObservableCollection()
. For the most part this works great, all the other code related to this works as expected, I can update the content of MyType from DataGrids, etc. The only problem is the TextBlock containing the property I want to display in the list, does not seem to update.
<ListBox ItemsSource="{Binding MyTypes}" SelectedItem="{Binding CurrentMyType}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I'm not sure why it is not updating, even if I add or remove an item from the bound collection. I was wondering if the Name
property needs to be an [ObservableProperty]
, but that doesn't seem like a good solution to me, as it would be mixing my data layer and presentation layer.
Upvotes: -1
Views: 48
Reputation: 20086
Why would it update? Bindings aren't magic. The binding can set properties on MyType
objects by using normal setters, but if MyType
does not implement INotifyPropertyChanged
(or provide per property change events), the binding has nothing to subscribe to, and neither does the observable collection. A MyType
object can be modified by anyone with a reference to the object; bindings and observable collections can't track that.
Upvotes: 0