当新实体添加到 DomainContext 的实体集中时,Silverlight 4 Datagrid 不会更新。 (绑定问题?)
我有一个绑定到 DomainDataSource 的 DataGrid:
<sdk:DataGrid AutoGenerateColumns="False" Height="Auto"
ItemsSource="{Binding ElementName=mailboxDomainDataSource, Path=Data,Mode=TwoWay}"
Name="mailboxHeaderDataGrid"....>...</sdk>
我还有一个添加按钮来添加新行:
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);
}
}
现在,当我迭代 DomainContext.Mailboxes 时,如函数末尾所示,新邮箱存在。
当我查看数据库时,新邮箱已存在。
如果我刷新页面,新邮箱就会出现在 DataGrid 中。
但是,当我迭代 ItemsSource 时,新邮箱不会出现(它不应该与 DomainContext.Mailboxes 相同吗,因为我将它们设置为相等?)。并且新邮箱不会出现在网格中。
任何帮助都会很棒。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试在代码中添加以下行,在分配新项目之前设置 ItemsSource = null:
但是,查看 xaml 和代码,我认为您存在一些不一致之处。
在您的 xaml 中,您使用
Binding ElementName=mailboxDomainDataSource, Path=Data
,并在您的代码中分配一个新的 ItemsSource。我相信更简洁的方法是使用 ObservableCollection(命名空间 System.Collections.ObjectModel)作为 ItemsSource 并用 DomainDataSource 的结果填充它。使用 DomainDataSource.LoadedData-Event 并复制结果。 (类似这样的,没有测试过:)
然后,当你添加一个新的邮箱时,你可以这样做:
You could try adding the following line in your code, setting the ItemsSource = null before assigning a new one:
However, looking at the xaml and the code I think you have some inconsistency there.
In your xaml you use
Binding ElementName=mailboxDomainDataSource, Path=Data
and in your code you assign a new ItemsSource.I believe a cleaner way would be to use an
ObservableCollection<Mailbox>
(namespace System.Collections.ObjectModel) as the ItemsSource and fill that with the results of the DomainDataSource. Use the DomainDataSource.LoadedData-Event and copy the results. (Something like this, not tested:)Then, when you add a new Mailbox, you can do this:
这是更好的方法,
This is better way to do it,
我最喜欢的方法如下:
或者,您可以使用事件参数来获取添加的实体,将其转换为邮箱并将其传递到 ItemsSource - 这只是一种懒惰的方法。
不妨利用 C# 事件驱动的优势!
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!