【问题标题】:C# Entity Framework - Data bound combobox does not reflect changes made after .SaveChanges()C# 实体框架 - 数据绑定组合框不反映 .SaveChanges() 之后所做的更改
【发布时间】:2010-07-15 14:01:35
【问题描述】:

好吧,这两天让我发疯了!

我是 C# 新手,并且一直通过编写一个简单的应用程序自学。

我有一个由一个组合框和两个文本框组成的简单表单。组合框包含数据库表中的实体列表。文本框允许用户添加新条目。它只是一个名称列表(名字和姓氏)。

表单上有添加、修改和删除三个按钮。

在幕后我使用数据绑定和 WPF。

好吧,我的问题是这个..

对于删除和修改操作,一切都如我所料。数据库会相应更改,并且(重要的是)组合框会立即反映对数据绑定实体所做的更改。

但是当我创建和添加一个新实体时,数据库会使用新项目进行正确更新,但是组合框不会在其列表中显示新实体(名称)。您必须退出表单并返回,才能看到组合框正确地反映了带有新添加项目的数据库表。

谁能告诉我让数据绑定控件反映其绑定到的表上的 INSERT 更改的正确做法是什么?

相关代码sn-ps如下...

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.myContext = new myEntities();
        // bind the contents of the table to the combobox
        myComboBox.DataContext = myContext.myPeople;
    }

     private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Update the text boxes to reflect the currently selected name
        this.person = myComboBox.SelectedItem as myPerson;

        if (this.person != null)
        {
            tbFirstName.Text = this.person.Firstname;
            tbSurname.Text = this.person.Surname;
        }
    }

    //User actions...

    if (userAction == crudAction.Modify)
    {
         // Update via the Entity Framework
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         msg = "Person details modified";
    }

    if (userAction == crudAction.Add)
    {
         person = new myPerson();
         person.Firstname = tbFirstName.Text;
         person.Surname = tbSurname.Text;
         person.idPeople = 0; //Autoincremented db key
         myContext.myPeople.AddObject(person);

         msg = "New person added";
    }

    if (userAction == crudAction.Delete)
    {
         myContext.myPeople.DeleteObject(person);
         msg = "Person deleted";
    }
    myContext.SaveChanges();

【问题讨论】:

    标签: c# wpf entity-framework binding


    【解决方案1】:

    如果您在运行SaveChanges() 后重新查询数据库,它会更新吗?

    var test = myContext.myPeople.ToList();
    

    编辑:

    再次为ComboBox 设置ItemsSource

    myComboBox.ItemsSource = context.myPeople.ToList();
    

    【讨论】:

    • 如果我在调用 .Savechanges() 之后添加该行,则没有区别 - 组合框不会反映新添加的名称
    • 啊!是的,有效!那是这种功能的标准做法吗?你能向我解释一下为什么会这样吗?
    • @Chris,它起作用的原因是组合框无法知道数据库中的数据何时发生变化。您必须刷新您提供给它的数据才能让它知道这一点。当您调用 myContext.SaveChanges 时,您正在更新数据库而不是 myComboBox.ItemsSource
    【解决方案2】:

    您可以使用事件来更新显示的父页面上的 Combobox SourceItems

    即在您处理创建新项目的自定义控件或表单中

        public static readonly RoutedEvent NewItemAddedEvent =
            EventManager.RegisterRoutedEvent("NewItemAdded", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(CloseableTabItem));
    
        public event RoutedEventHandler NewItemAdded
        {
            add { AddHandler(NewItemAddedEvent, value); }
            remove { RemoveHandler(NewItemAddedEvent, value); }
        }
    

    private void SaveButton_Click(object sender, RoutedEventArgs e) { ProActive.Contact currentContact = (ProActive.Contact)ItemsListBox.Items.CurrentItem;

            switch (MessageBox.Show("Are you sure?", "Save Changes", MessageBoxButton.YesNoCancel))
            {
                case MessageBoxResult.Yes:
                    if (currentContact.EntityState == System.Data.EntityState.Detached)
                        ProActive.App.ProActiveDatabaseEntities.Contacts.AddObject(currentContact);
                    ProActive.App.ProActiveDatabaseEntities.SaveChanges();
    
                    this.RaiseEvent(new RoutedEventArgs(NewItemAddedEvent, this));
    
                    break;
    }
    

    然后在显示组合框的母版页中,附加在“保存”新项目后触发的事件

    ProActive.TabPagesControls.AllContactsDetailsControl cdc = new AllContactsDetailsControl();
    cdc.NewItemAdded += AllContactsDetailsNewItemAdded;
    

    然后在它被触发后处理父页面上的事件以重新加载 itemssource

      private void AllContactsDetailsNewItemAdded(object sender, RoutedEventArgs e)
        {
            // New item added so refresh the items listbox
            AllContactListItemsListBox.ItemsSource = from c in  ProoActive.App.ProActiveDatabaseEntities.Contacts select c;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多