1

I have a data bound list box that was built using the EF 4.1 model first. So all my classes where built for me. Because I have three controls that reflect the table data that traces back from the foreign key. this.lstBox2.ItemSource = entityContext.TableObject2.ToList() will return every record. Not the records that the M-D are showing limited by the foreign key constraint.

TableObject2 class2 = new TableObject2();
class2.value1 = 0;
class2.value2 = "new location";

using (TKOEntities entityContext = new TKOEntities())
{
                entityContext.TableObject2.AddObject(class2);
                entityContext.SaveChanges();
                this.lstBox2.ItemsSource = null;
}

SaveChanges does update the data into the database. But the control is not refreshed (this.lstBox2.Refresh() doesn't work). If I try to set the value to the control. I also get the us ItemControl.ItemSource error. How do I assign the control the updated values saved to the entitycontext?

2 Answers2

1

The model changes are not automatically propagated to the view-model or your view (as I can see you don't have a MVVM view-model?)

You'd normally need to Bind and do PropertyChanged.

This is writing from a device and very rough and fast (I may have mistyped something etc.)...

<ListBox ItemsSource={Binding YourCollectionProperty} >

in your 'view-model' (or if that's your 'control', which I don't recommend, then do something like {Binding ElementName=_mywin, Path=YourCollectionProperty}) define the property as...

public ObservableCollection<POCOItem> YourCollectionProperty 
{
get
{
     return _collection ?? (_collection = WrapModel());
}
set
{}}  

Implement IPropertyChanged interface When your model is updated...

_collection = null;
OnPropertyChanged("YourCollectionProperty");

ObservableCollection is useless here (an array/list will do just the same - keeping model collection in sync isn't easy.

So, if you need to be closely tied to your model - you could make your model navigation property ICollection to be ObservableCollection (though there're pros and cons but not to get into that).

e.g. see this one Do I need to implement INotifyPropertyChanged when using EF Code-First?

In that case - when just adding new items - that should go automatically to you ListBox.

If you refresh the collection - then do the above (set PropertyChanged for the collection property)

Item Properties are not automatically updated - unless you implement IPropertyChanged on your model as well.

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

Instead of:

this.lstBox2.ItemsSource = null;

Try fetching the data from entityContext after you save the changes to rebuild your list:

this.lstBox2.ItemsSource = entityContext.TableObject2.ToList();

There is a better approach you could take I'm sure, but to quickly get your databack out of the db and into the list, this should work.

Also a quick code bit, you can use object initializer:

TableObject2 class2 = new TableObject2 { value1 = 0, value2 = "new location" };
crizzwald
  • 1,037
  • 2
  • 14
  • 30
  • this.lstBox2.ItemsSource = entityContext.TableObject2.ToList(); Will return everything back and does reload the data in the control but it is everything and not limited to what is selected on the first control by Master-Detail. – Greg Fredrickson Apr 04 '13 at 16:43
  • Then you'll need to apply a select filter prior to doing the ToList() and pass in what the first control has as its selected value. – crizzwald Apr 04 '13 at 16:45
  • or, use the Include method and pass in TableObject2 as its selector to get the related TableObject2 objects by fk. var obj = entityContext.MDTable.Include("TableObject2").FirstOrDefault(x => x.Id = slected.Id); this.lstBox2.ItemsSource = obj.TableObject2s.ToList(); – crizzwald Apr 04 '13 at 16:52