I have a DataGrid bound to a DomainDataSource:
<sdk:DataGrid AutoGenerateColumns="False" Height="Auto"
ItemsSource="{Binding ElementName=mailboxDomainDataSource, Path=Data,Mode=TwoWay}"
Name="mailboxHeaderDataGrid"....>...</sdk>
I also have an add button to add a new row:
private void addMailboxButton_Click(object sender, RoutedEventArgs e)
{
Mailbox m = new Mailbox();
InboxNotifierDomainContext context = (InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext;
((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes.Add(m);
if (!mailboxDomainDataSource.DomainContext.IsSubmitting) if (mailboxDomainDataSource.HasChanges) mailboxDomainDataSource.SubmitChanges();
mailboxHeaderDataGrid.ItemsSource = ((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes;
foreach (Mailbox m1 in ((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes)
{
MessageBox.Show(m1.MailboxID + '-' + m1.MailBox1);
}
}
Now, when I iterate through DomainContext.Mailboxes, as at the end of the function, the new mailbox exists.
When I look in my database, the new mailbox exists.
If I refresh the page, the new mailbox appears in the DataGrid.
However, when I iterate through the ItemsSource, the new mailbox doesn’t appear (shouldn’t it be the same as DomainContext.Mailboxes, since I set them equal?). And the new mailbox doesn’t appear in the grid.
Any help would be wonderful.
Thanks in advance!
My favourite way to do this is as follows:
Alternatively, you can use the event args to get the added entity, cast it as a mailbox and and it to the ItemsSource–this was just the lazy way.
Might as well take advantage of C# being event-driven!