Problem is in the way you are creating FourItems view. Here in this line -
ContactCard CC = new ContactCard();
CC.DataContext = FullObjects.contacts[i];
if (i == 0)
{
FI.FirstItem.Children.Add(CC);
}
you are setting DataContext
explicitly for ContactCard
and later on changed it completely to new reference which obviously won't get reflect on UI unless you manually reset DataContext.
Instead you should have an ItemsControl
and bind its ItemsSource
directly to Contacts ObservableCollection
. You can define ItemTemplate
of itemsControl to represent one ContactCard. This way you will get CollectionChanged
notification of observable collection and UI will gets update automatically.
View to hold four controls (Get rid of stackpanels) -
<Grid>
<ItemsControl x:Name="ContactItems">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ContactCard/> <-- Declare local namespace in XAML where
ContactCard control is declared.
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
And populate code should be like this -
FourItems FI = new FourItems();
FI.ContactItems.ItemsSource = FullObjects.contacts;
FlipView.Items.Add(FI);
Now any update in collection contacts will be listen by ItemsControl since its binded to it and your UI will update accordingly. Make sure contacts is ObservableCollection
.