<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx</link><description>The ObservableCollection is a special WPF collection that provides proper notifications to the UI when items are added, removed, or the list is refreshed because it implements INotifyCollectionChanged . It’s common to use this collection (or inherit from</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#10410886</link><pubDate>Sun, 14 Apr 2013 10:38:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10410886</guid><dc:creator>dush1</dc:creator><description>&lt;p&gt;Can we have a c# version of this code ? I tried Telerick vb to c# converter but the compiler gives an error.&lt;/p&gt;
&lt;p&gt; &amp;nbsp;protected override void RemoveItem(int index)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Context.DeleteObject(this(index));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;base.RemoveItem(index);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Error says &amp;quot;Method name expected&amp;quot; for &amp;quot;this&amp;quot; inside DeleteObject method call&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10410886" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#10108120</link><pubDate>Wed, 22 Dec 2010 13:10:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10108120</guid><dc:creator>John</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Is it usable for binding binary data like images which are stored in DB?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10108120" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#10089690</link><pubDate>Thu, 11 Nov 2010 18:33:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10089690</guid><dc:creator>Steve B</dc:creator><description>&lt;p&gt;Hi Beth, &lt;/p&gt;
&lt;p&gt;Thanks for your very interesting article. Overriding the InsertItem and RemoveItem methods in the ObservableCollection certainly is an easy way to get the data into your ObjectContext from a System.Windows.Controls.DataGrid. &lt;/p&gt;
&lt;p&gt;However, using this method, when a new row is added in the DataGrid the data goes directly into your ObjectContext (and from there, presumably, into your database). This is problematic if you want to allow the user to add a row, make some edits, and then either commit or cancel the add operation. (The commit or cancel are invoked by moving to a new row or by pressing ESC twice). InsertItem will be called by ListCollectionView.AddNew, and RemoveItem will be called by CancelAdd, but this does not fully reflect the semantics of AddNew / CancelNew / CommitNew. For example, if the power goes out or if the user goes out to lunch, you can easily end up with an incompletely edited row in your database. &lt;/p&gt;
&lt;p&gt;One way to address these questions would be to override ListCollectionView.AddNew, CommitAdd, and CancelAdd and keep the new row in a buffer unti the CommitAdd occurs. Unfortunately, these functions are not virtual .... at least not in the usual sense. They are declared in one of the interfaces that ListCollectionView implements. Of course one could try to re-implement ListCollectionView by forwarding all the method calls, but that is obviously a big hassle. &lt;/p&gt;
&lt;p&gt;What I recently found out is that interface members can be overridden, with one important caveat: the overriding function can only be called through the interface, not the base class. There&amp;#39;s an excellent article about it in &lt;a rel="nofollow" target="_new" href="http://www.williamcaputo.com/archives/000138.html"&gt;www.williamcaputo.com/.../000138.html&lt;/a&gt; . In the context of collection views, what we can do is derive from ListCollectionView and implement an interface, let&amp;#39;s call it IListCollectionView, that consists of all the interfaces already implemented by ListCollectionView (and its base classes). Seems a bit redundant, I agree ... but it allows us to redefine IListCollectionView.AddNew/CancelNew/CommitNew without having to explicitly redefine all the other members and properties of ListCollectionView. &lt;/p&gt;
&lt;p&gt;What we can do is use IListCollectionView as the type of object returned by the view model (MVVM paradigm). We just bind our DataGrid to the IListCollectionView, and when it calls AddNew/CommitNew/CancelNew it will invoke the overridden function rather than the original.&lt;/p&gt;
&lt;p&gt;Best wishes, &lt;/p&gt;
&lt;p&gt;Steve&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10089690" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#10065688</link><pubDate>Tue, 21 Sep 2010 14:52:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10065688</guid><dc:creator>pietermuller</dc:creator><description>&lt;p&gt;Thanks, this is the simplest workaround I have yet seen for a decent EDM-WPF link! Awesome.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10065688" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9931818</link><pubDate>Thu, 03 Dec 2009 06:09:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931818</guid><dc:creator>sumptert</dc:creator><description>&lt;p&gt;Using your collection example with Linq2SQL I am having an issue with the timestamp not updating from database. &amp;nbsp;The timestamp is required for the stored procedure to be refreshed after update.&lt;/p&gt;
&lt;p&gt;The Timestamp retreival works if I dont use the collection.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931818" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9907906</link><pubDate>Fri, 16 Oct 2009 01:10:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9907906</guid><dc:creator>gill bate</dc:creator><description>&lt;p&gt;Thanks! &lt;/p&gt;
&lt;p&gt;Here is the VB version of it.&lt;/p&gt;
&lt;p&gt;Imports System.ComponentModel&lt;/p&gt;
&lt;p&gt;Imports System.Collections.ObjectModel&lt;/p&gt;
&lt;p&gt;Public Class SortableObservableCollection(Of T)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Inherits ObservableCollection(Of T)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Overloads Sub Sort(Of TKey)(ByVal KeySelector As Func(Of T, TKey))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sort(KeySelector, ListSortDirection.Ascending)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Overloads Sub Sort(Of TKey)(ByVal KeySelector As Func(Of T, TKey), ByVal dir As ListSortDirection)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If dir = ListSortDirection.Ascending Then&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ApplySort(Items.OrderBy(KeySelector))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ApplySort(Items.OrderByDescending(KeySelector))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Overloads Sub Sort(Of TKey)(ByVal KeySelector As Func(Of T, TKey), ByVal comparer As IComparer(Of TKey))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ApplySort(Items.OrderBy(KeySelector, comparer))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Private Sub ApplySort(ByVal sortedItems As IEnumerable(Of T))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim lst = sortedItems.ToList &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;For Each item As T In lst&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Move(IndexOf(item), lst.IndexOf(item))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Next&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;Example of how to use it:&lt;/p&gt;
&lt;p&gt;Public Class Chicken&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Property Name() As String&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;......&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;Public Class IHasLotsaChickens&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Inherits SortableObservableCollection(Of Chicken)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Sub SortMaChickens()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Me.Sort(Function(chic) chic.Name, ListSortDirection.Descending)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Sub&lt;/p&gt;
&lt;p&gt;End Class&lt;/p&gt;
&lt;p&gt;haha :D&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9907906" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9793079</link><pubDate>Sat, 20 Jun 2009 10:40:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793079</guid><dc:creator>csaket</dc:creator><description>&lt;p&gt;A question.&lt;/p&gt;
&lt;p&gt;This is great when the action is initiated by the user and thus through the UI.&lt;/p&gt;
&lt;p&gt;What about the other way? If there is an update in the database, how to propogate that to the UI?&lt;/p&gt;
&lt;p&gt;Example there would be a timer where a lookup is made to the database and data pulled up.&lt;/p&gt;
&lt;p&gt;Where do we go about changing the collection so that the new/changed data reflects on the ui.&lt;/p&gt;
&lt;p&gt;What I am doing now is not elegant and would like to know a best practices way of doing this and if you know of any posts that go through this.&lt;/p&gt;
&lt;p&gt;(right now I am pulling up a list of simple objects that contain the data over wcf and then generating an observable collection and setting up the ui through a collectionviewsource. On the update thread another collection comes through and now I have to reconcile the two. Just looking to know what would be the best approach.)&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9793079" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9624148</link><pubDate>Sun, 17 May 2009 11:36:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9624148</guid><dc:creator>Robert Reijntjes</dc:creator><description>&lt;p&gt;I created a master/detail viewmodel based on your articles and your video. Below is the link to an article about it on my blog:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://reyntjes.blogspot.com/2009/04/master-detail-viewmodel_24.html"&gt;http://reyntjes.blogspot.com/2009/04/master-detail-viewmodel_24.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Maybe you like it.&lt;/p&gt;
&lt;p&gt;Anyway, thanks for the articles.&lt;/p&gt;
&lt;p&gt;Robert&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9624148" width="1" height="1"&gt;</description></item><item><title>Building WPF Business Applications using Entity Framework</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9620685</link><pubDate>Sat, 16 May 2009 05:06:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9620685</guid><dc:creator>Visual Studio Data</dc:creator><description>&lt;p&gt;I’ve been writing a lot about building WPF business applications with Entity Framework using Visual Studio&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9620685" width="1" height="1"&gt;</description></item><item><title>re: Using the WPF ObservableCollection with EF Entities</title><link>http://blogs.msdn.com/b/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx#9607493</link><pubDate>Tue, 12 May 2009 18:50:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9607493</guid><dc:creator>Beth Massi</dc:creator><description>&lt;p&gt;Hi Keith,&lt;/p&gt;
&lt;p&gt;I totally forgot about the base constructor, thanks! I updated the code. You may want to expose IsLoading as a readonly property for some other reason but we don't need it here. &lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;-B &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9607493" width="1" height="1"&gt;</description></item></channel></rss>