Imagine I have an entity:
public class MyObject
{
public string Name { get; set; }
}
And I have a ListBox:
<ListBox x:Name="lbParts">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I bind it to a collection in code-behind:
ObjectQuery<MyObject> componentQuery = context.MyObjectSet;
Binding b = new Binding();
b.Source = componentQuery;
lbParts.SetBinding(ListBox.ItemsSourceProperty, b);
And the on a button click I add an entity to the MyObjectSet:
var myObject = new MyObject { Name = "Test" };
context.AddToMyObjectSet(myObject);
Here is the problem – this object needs to update in the UI to. But it is not added there 🙁
The
ObjectQuery<T>class doesn’t implement theINotifyCollectionChangedinterface, so it doesn’t notify the UI when an item is added or removed. You need to use anObservableCollection<T>which is a copy of yourObjectQuery<T>; when you add an item to theObjectQuery<T>, also add it to theObservableCollection<T>.Binding :
Add item :