C#Entity Framework - 数据绑定组合框不反映.SaveChanges()之后所做的更改

时间:2010-07-15 14:01:36

标签: c# wpf entity-framework binding

好的,过去两天这让我疯了!

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

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

在表单上有三个按钮,分别为添加,修改和删除。

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

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

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

但是当我创建并添加新实体时,数据库会更新与新项目相关但组合框不会在其列表中显示新实体(名称)。您必须退出表单并返回,以便看到组合框正确反映了新添加项目的数据库表。

有人能告诉我正确的做法是什么让数据绑定控件反映它所绑定的表上的INSERT更改?

以下相关代码段......

    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();

2 个答案:

答案 0 :(得分:1)

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

var test = myContext.myPeople.ToList();

修改

再次为ItemsSource设置ComboBox

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

答案 1 :(得分:0)

您可以使用事件更新显示

的父页面上的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;
    }