sony
sony

Reputation: 1503

Entity Framework - update datacontext not updating UI?

In my WPF appliaction, I use entity framework. I set the datacontext of the main grid to an object of GroupTxt.

<Grid Name="main">
  <!-- Textboxes with binding -->
</Grid>


  DataEntities dt = new DataEntities();
  GroupTxt objGroupTxt; 

  void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        objGroupTxt= dt.GroupTxts.First();
        main.DataContext = objGroupTxt;
    }

I then pass the object to an external dll where its processed and then return the object back. I then asign this object to objGroupTxt:

objGroupTxt= modifiedGroupTxt // object modified by external dll

but the changed values are not updated in the bound text boxes.

Upvotes: 0

Views: 1292

Answers (2)

John Bowen
John Bowen

Reputation: 24453

It looks like you're confusing the behavior of reference type assignments with the behavior of bindings. When you initially assign a value to objGroupTxt (call this instance A), objGroupTxt is now pointing to A. You next assign objGroupTxt (A) to main.DataContext, which will now also be pointing to A. It's not clear from what you posted but it looks like what you're getting from the processing as modifiedGroupTxt is a new instance (B). You're then reassigning objGroupTxt to B, but main.DataContext is still using A.

You can correct this by just reassigning DataContext every time you get a new processed instance or set up objGroupTxt as a property with change notification (INotifyPropertyChanged or DependencyProperty) that you can then Bind DataContext to.

Upvotes: 2

Justin Pihony
Justin Pihony

Reputation: 67115

Without more information than is already provided (you may need to provide more code for a fuller answer), I would guess that your object does not implement the INotifyPropertyChanged interface. Also, you are not setting your context to the dt, but instead to an object inside the dt, so changing the dt would not do anything.

Upvotes: 0

Related Questions