当新实体添加到 DomainContext 的实体集中时,Silverlight 4 Datagrid 不会更新。 (绑定问题?)

发布于 2024-10-19 07:50:48 字数 1379 浏览 1 评论 0原文

我有一个绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幼儿园老大 2024-10-26 07:50:48

您可以尝试在代码中添加以下行,在分配新项目之前设置 ItemsSource = null:

// Set ItemsSource to null
mailboxHeaderDataGrid.ItemsSource = null;
// Assign new ItemsSource
mailboxHeaderDataGrid.ItemsSource = ((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes;

但是,查看 xaml 和代码,我认为您存在一些不一致之处。
在您的 xaml 中,您使用 Binding ElementName=mailboxDomainDataSource, Path=Data ,并在您的代码中分配一个新的 ItemsSource。

我相信更简洁的方法是使用 ObservableCollection(命名空间 System.Collections.ObjectModel)作为 ItemsSource 并用 DomainDataSource 的结果填充它。使用 DomainDataSource.LoadedData-Event 并复制结果。 (类似这样的,没有测试过:)

private ObservableCollection<Mailbox> _mailboxCollection = new ObservableCollection<Mailbox>();
public ObservableCollection<Mailbox> MailboxCollection { ... }

private void DomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
{
  if (e.HasError)
  {
    // Error handling
  }
  else
  {
    if (e.Entities.Count > 0)
    {
      this.MailboxCollection = new ObservableCollection(e.Entities);
    }
  }
}

然后,当你添加一个新的邮箱时,你可以这样做:

((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes.Add(m);
this.MailboxObservableCollection.Add(m);

You could try adding the following line in your code, setting the ItemsSource = null before assigning a new one:

// Set ItemsSource to null
mailboxHeaderDataGrid.ItemsSource = null;
// Assign new ItemsSource
mailboxHeaderDataGrid.ItemsSource = ((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes;

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:)

private ObservableCollection<Mailbox> _mailboxCollection = new ObservableCollection<Mailbox>();
public ObservableCollection<Mailbox> MailboxCollection { ... }

private void DomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
{
  if (e.HasError)
  {
    // Error handling
  }
  else
  {
    if (e.Entities.Count > 0)
    {
      this.MailboxCollection = new ObservableCollection(e.Entities);
    }
  }
}

Then, when you add a new Mailbox, you can do this:

((InboxNotifierDomainContext)mailboxDomainDataSource.DomainContext).Mailboxes.Add(m);
this.MailboxObservableCollection.Add(m);
怪异←思 2024-10-26 07:50:48

这是更好的方法,

private void addMailboxButton_Click(object sender, RoutedEventArgs e)
{
     Mailbox m = new Mailbox();
     mailboxDomainDataSource.DataView.Add(m);
     // it takes a little while for Datagrid to update so 
     // we will queue our selection logic
     Dispatcher.BeginInvoke( ()=>{
         mailBoxGrid.SelectedItem = m;
     } );
}

This is better way to do it,

private void addMailboxButton_Click(object sender, RoutedEventArgs e)
{
     Mailbox m = new Mailbox();
     mailboxDomainDataSource.DataView.Add(m);
     // it takes a little while for Datagrid to update so 
     // we will queue our selection logic
     Dispatcher.BeginInvoke( ()=>{
         mailBoxGrid.SelectedItem = m;
     } );
}
满意归宿 2024-10-26 07:50:48

我最喜欢的方法如下:

InboxNotifierDomainContext _context = mailboxDomainDataSource.DomainContext as InboxNotifierDomainContext;

_context.EntityContainer.GetEntitySet<Mailbox>().EntityAdded += (s,e) => {mailBoxGrid.ItemsSource = mailboxDomainDataSource.DomainContext.EntityContainer.GetEntitySet<Mailbox>();};

或者,您可以使用事件参数来获取添加的实体,将其转换为邮箱并将其传递到 ItemsSource - 这只是一种懒惰的方法。

不妨利用 C# 事件驱动的优势!

My favourite way to do this is as follows:

InboxNotifierDomainContext _context = mailboxDomainDataSource.DomainContext as InboxNotifierDomainContext;

_context.EntityContainer.GetEntitySet<Mailbox>().EntityAdded += (s,e) => {mailBoxGrid.ItemsSource = mailboxDomainDataSource.DomainContext.EntityContainer.GetEntitySet<Mailbox>();};

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文