<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="http://code.msdn.microsoft.com/rss.xsl"?><rss version="2.0"><channel><title>WPF ListWrappingObservableCollection</title><link>http://code.msdn.microsoft.com/wpfbindablelistwrap/Project/ProjectRss.aspx</link><description>This page shows how to get the features of a List &amp;#40;e.g. serializable, complex search capabilities&amp;#41; with the databinding screen updating &amp;#39;magic&amp;#39; of an ObservableCollection.</description><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/wpfbindablelistwrap/Wiki/View.aspx?title=Home&amp;version=5</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;This page shows how to get the features of a List &amp;#40;e.g. serializable, complex search capabilities&amp;#41; with the databinding screen updating &amp;#39;magic&amp;#39; of an ObservableCollection.
&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The problem
&lt;/h5&gt;You want to use generic lists (List&amp;lt;T&amp;gt;) to handle collections within your application.&lt;br /&gt;&lt;ul&gt;
&lt;li&gt;Because they are serializable (e.g. you can pass a List&amp;lt;T&amp;gt; of data contracts to/from a WCF service)&lt;/li&gt;&lt;li&gt;They have extensive search capability (e.g. you can find / replace items using Predicates)&lt;/li&gt;&lt;li&gt;I'm sure there are some other good reasons ...&lt;/li&gt;
&lt;/ul&gt;However, if you databind a List&amp;lt;T&amp;gt; to an ItemSource in a WPF control (e.g. ListView) then adding or updating entries in the&lt;br /&gt;List&amp;lt;T&amp;gt; does not cause the display to update (at least not without a bit of a hack to fool the UI into thinking that the&lt;br /&gt;bound property (DependencyProperty or property using INotifyPropertyChanged) holding the List&amp;lt;T&amp;gt; has changed).&lt;br /&gt; &lt;br /&gt;The normal solution is to make the databound property an ObservableCollection&amp;lt;T&amp;gt; (which can take a List&amp;lt;T&amp;gt; in it's constructor).&lt;br /&gt;As you add / remove / update items in this collection within the application, the screen automagically updates.&lt;br /&gt;However, to transfer data back to the service layer you need to copy the items back into a List&amp;lt;T&amp;gt; - this is because the ObservableCollection&lt;br /&gt;constructor simply copies the items in the List&amp;lt;T&amp;gt; to it's own inner list (i.e. it does not retain a reference to the original List&amp;lt;T&amp;gt; passed in).&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The solution
&lt;/h5&gt;We want an ObservableCollection&amp;lt;T&amp;gt; which maintains a reference to the original List&amp;lt;T&amp;gt; passed into it's constructor, then any changes made&lt;br /&gt;to items in this collection will be automatically applied to the List&amp;lt;T&amp;gt;.&lt;br /&gt;We can then simply pass the inner list or a (data contract containing the inner list) back though the service layer to update the database.&lt;br /&gt; &lt;br /&gt;Here is the code for a ListWrappingObservableCollection&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
 
namespace WpfSamples
{
	/// &amp;lt;summary&amp;gt;
	/// WPF bindable collection which overrides the behaviour of ObservableCollection which 'copies' the items in a list to it's inner list.&amp;lt;br/&amp;gt;
	/// We want the list to remain intact so that any references to it are maintained.
	/// &amp;lt;/summary&amp;gt;
	/// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
	public class ListWrappingObservableCollection&amp;lt;T&amp;gt; : ObservableCollection&amp;lt;T&amp;gt;
	{
		public ListWrappingObservableCollection(List&amp;lt;T&amp;gt; innerList) : base()
		{
			if (innerList == null)
				throw new ArgumentNullException(&amp;quot;innerList&amp;quot;);
 
			// Get a reference to the private items variable in Collection&amp;lt;T&amp;gt; and set it to the passed in List&amp;lt;T&amp;gt;
			FieldInfo field = typeof(Collection&amp;lt;T&amp;gt;).GetField(&amp;quot;items&amp;quot;, System.Reflection.BindingFlags.NonPublic
			 | System.Reflection.BindingFlags.Instance);
			if (field != null)
				field.SetValue(this, innerList);
		}
 
		/// &amp;lt;summary&amp;gt;
		/// Gets the inner list.
		/// &amp;lt;/summary&amp;gt;
		/// &amp;lt;value&amp;gt;The inner list.&amp;lt;/value&amp;gt;
		public List&amp;lt;T&amp;gt; InnerList
		{
			get { return this.Items as List&amp;lt;T&amp;gt;; }
		}
 
	}
}
&lt;/pre&gt;When manipulating items in the collection - use the appropriate methods of the ListWrappingObservableCollection itself (not the InnerList) - this will indirectly&lt;br /&gt;update the inner List&amp;lt;T&amp;gt; and trigger the 'magic' which updates the item in the UI.&lt;br /&gt; &lt;br /&gt;The following code snippet defines a simplified data contract for an Order which you could use to pass the data needed to maintain an order and it's&lt;br /&gt;items between application layers.&lt;br /&gt;&lt;pre&gt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
 
namespace WpfSamples
{
	[Serializable]
	[DataContract]
	public class Order : INotifyPropertyChanged
	{
		private int orderId;
		[DataMember]
		public int OrderId
		{
			get { return orderId; }
			set
			{
				orderId = value;
				OnPropertyChanged(&amp;quot;OrderId&amp;quot;);
			}
		}
 
		private bool isDeleted;
		[DataMember]
		public System.Boolean IsDeleted
		{
			get { return isDeleted; }
			set
			{
				isDeleted = value;
				OnPropertyChanged(&amp;quot;IsDeleted&amp;quot;);
			}
		}
 
		private List&amp;lt;OrderItem&amp;gt; orderItems;
		[DataMember]
		public List&amp;lt;OrderItem&amp;gt; OrderItems
		{
			get { return orderItems; }
			set
			{
				orderItems = value;
				if (orderItems != null)
					this.OrderItemsBindable = new ListWrappingObservableCollection&amp;lt;OrderItems&amp;gt;(orderItems);
				else
					this.OrderItemsBindable = null;
 
				OnPropertyChanged(&amp;quot;OrderItems&amp;quot;);
			}
		}
 
		private ListWrappingObservableCollection&amp;lt;OrderItem&amp;gt; orderItemsBindable;
		public ListWrappingObservableCollection&amp;lt;OrderItem&amp;gt; OrderItemsBindable
		{
			get { return orderItemsBindable; }
			private set
			{
				orderItemsBindable = value;
				OnPropertyChanged(&amp;quot;OrderItemsBindable&amp;quot;);
			}
		}
 
		/// &amp;lt;summary&amp;gt;
		/// Called when [property changed].
		/// &amp;lt;/summary&amp;gt;
		/// &amp;lt;param name=&amp;quot;name&amp;quot;&amp;gt;The name of the property which changed.&amp;lt;/param&amp;gt;
		protected void OnPropertyChanged(string name)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(name));
			}
		}
 
 
		#region INotifyPropertyChanged Members
 
		[field: NonSerialized]
		public event PropertyChangedEventHandler PropertyChanged;
 
		#endregion INotifyPropertyChanged Members
	}
}
&lt;/pre&gt;In the UI, simply databind to the OrderItemsBindable property and add / update items in this collection and the OrderItems property will&lt;br /&gt;automatically get updated as appropriate.&lt;br /&gt;The Order data contract can then be passed back through the application layers (e.g. to update a database).&lt;br /&gt;
&lt;/div&gt;</description><author>hortha</author><pubDate>Tue, 06 May 2008 13:40:28 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080506P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/wpfbindablelistwrap/Wiki/View.aspx?title=Home&amp;version=4</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;This page shows how to get the features of a List &amp;#40;e.g. serializable, complex search capabilities&amp;#41; with the databinding screen updating &amp;#39;magic&amp;#39; of an ObservableCollection.
&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The problem
&lt;/h5&gt;You want to use generic lists (List&amp;lt;T&amp;gt;) to handle collections within your application.&lt;br /&gt;&lt;ul&gt;
&lt;li&gt;Because they are serializable (e.g. you can pass a List&amp;lt;T&amp;gt; of data contracts to/from a WCF service)&lt;/li&gt;&lt;li&gt;They have extensive search capability (e.g. you can find / replace items using Predicates)&lt;/li&gt;&lt;li&gt;I'm sure there are some other good reasons ...&lt;/li&gt;
&lt;/ul&gt;However, if you databind a List&amp;lt;T&amp;gt; to an ItemSource in a WPF control (e.g. ListView) then adding or updating entries in the&lt;br /&gt;List&amp;lt;T&amp;gt; does not cause the display to update (at least not without a bit of a hack to fool the UI into thinking that the&lt;br /&gt;DependencyProperty holding the List&amp;lt;T&amp;gt; has changed).&lt;br /&gt; &lt;br /&gt;The normal solution is to make the databound property an ObservableCollection&amp;lt;T&amp;gt; (which can take a List&amp;lt;T&amp;gt; in it's constructor).&lt;br /&gt;As you add / remove / update items in this collection within the application, the screen automagically updates.&lt;br /&gt;However, to transfer data back to the service layer you need to copy the items back into a List&amp;lt;T&amp;gt; - this is because the ObservableCollection&lt;br /&gt;constructor simply copies the items in the List&amp;lt;T&amp;gt; to it's own inner list (i.e. it does not retain a reference to the original List&amp;lt;T&amp;gt; passed in)&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The solution
&lt;/h5&gt;We want an ObservableCollection&amp;lt;T&amp;gt; which maintains a reference to the original List&amp;lt;T&amp;gt; passed into it's constructor, then any changes made&lt;br /&gt;to items in this collection will be automatically applied to the List&amp;lt;T&amp;gt;.&lt;br /&gt;We can then simply pass the inner list or a (data contract containing the inner list) back though the service layer to update the database.&lt;br /&gt; &lt;br /&gt;Here is the code for a ListWrappingObservableCollection&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
 
namespace WpfSamples
{
	/// &amp;lt;summary&amp;gt;
	/// WPF bindable collection which overrides the behaviour of ObservableCollection which 'copies' the items in a list to it's inner list.&amp;lt;br/&amp;gt;
	/// We want the list to remain intact so that any references to it are maintained.
	/// &amp;lt;/summary&amp;gt;
	/// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;
	public class ListWrappingObservableCollection&amp;lt;T&amp;gt; : ObservableCollection&amp;lt;T&amp;gt;
	{
		public ListWrappingObservableCollection(List&amp;lt;T&amp;gt; innerList) : base()
		{
			if (innerList == null)
				throw new ArgumentNullException(&amp;quot;innerList&amp;quot;);
 
			// Get a reference to the private items variable in Collection&amp;lt;T&amp;gt; and set it to the passed in List&amp;lt;T&amp;gt;
			FieldInfo field = typeof(Collection&amp;lt;T&amp;gt;).GetField(&amp;quot;items&amp;quot;, System.Reflection.BindingFlags.NonPublic
			 | System.Reflection.BindingFlags.Instance);
			if (field != null)
				field.SetValue(this, innerList);
		}
 
		/// &amp;lt;summary&amp;gt;
		/// Gets the inner list.
		/// &amp;lt;/summary&amp;gt;
		/// &amp;lt;value&amp;gt;The inner list.&amp;lt;/value&amp;gt;
		public List&amp;lt;T&amp;gt; InnerList
		{
			get { return this.Items as List&amp;lt;T&amp;gt;; }
		}
 
	}
}
&lt;/pre&gt;
&lt;/div&gt;</description><author>hortha</author><pubDate>Mon, 05 May 2008 20:38:33 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080505P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/wpfbindablelistwrap/Wiki/View.aspx?title=Home&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;This page shows how to get the features of a List &amp;#40;e.g. serializable, complex search capabilities&amp;#41; with the databinding screen updating &amp;#39;magic&amp;#39; of an ObservableCollection.
&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The problem
&lt;/h5&gt;You want to use generic lists (List&amp;lt;T&amp;gt;) to handle collections within your application.&lt;br /&gt;&lt;ul&gt;
&lt;li&gt;Because they are serializable (e.g. you can pass a List&amp;lt;T&amp;gt; of data contracts to/from a WCF service)&lt;/li&gt;&lt;li&gt;They have extensive search capability (e.g. you can find / replace items using Predicates)&lt;/li&gt;&lt;li&gt;I'm sure there are some other good reasons ...&lt;/li&gt;
&lt;/ul&gt;However, if you databind a List&amp;lt;T&amp;gt; to an ItemSource in a WPF control (e.g. ListView) then adding or updating entries in the&lt;br /&gt;List&amp;lt;T&amp;gt; does not cause the display to update (at least not without a bit of a hack to fool the UI into thinking that the&lt;br /&gt;DependencyProperty holding the List&amp;lt;T&amp;gt; has changed).&lt;br /&gt; &lt;br /&gt;The normal solution is to make the databound property an ObservableCollection&amp;lt;T&amp;gt; (which can take a List&amp;lt;T&amp;gt; in it's constructor).&lt;br /&gt;As you add / remove / update items in this collection within the application, the screen automagically updates.&lt;br /&gt;However, to transfer data back to the service layer you need to copy the items back into a List&amp;lt;T&amp;gt; - this is because the ObservableCollection&lt;br /&gt;constructor simply copies the items in the List&amp;lt;T&amp;gt; to it's own inner list (i.e. it does not retain a reference to the original List&amp;lt;T&amp;gt; passed in)&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The solution
&lt;/h5&gt; &lt;br /&gt;
&lt;/div&gt;</description><author>hortha</author><pubDate>Mon, 05 May 2008 20:26:35 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080505P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/wpfbindablelistwrap/Wiki/View.aspx?title=Home&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Resource Page Description&lt;/b&gt;&lt;br /&gt;This page shows how to get the features of a List &amp;#40;e.g. serializable, complex search capabilities&amp;#41; with the databinding screen updating &amp;#39;magic&amp;#39; of an ObservableCollection.
&lt;br /&gt; &lt;br /&gt;&lt;h5&gt;
The problem
&lt;/h5&gt;You want to use generic lists (List&amp;lt;T&amp;gt;) to handle collections within your application.&lt;br /&gt;&lt;ul&gt;
&lt;li&gt;Because they are serializable (e.g. you can pass a List&amp;lt;T&amp;gt; of data contracts to/from a WCF service)&lt;/li&gt;&lt;li&gt;They have extensive search capability (e.g. you can find / replace items using Predicates)&lt;/li&gt;&lt;li&gt;I'm sure there are some other good reasons ...&lt;/li&gt;
&lt;/ul&gt; &lt;br /&gt;However, if you databind a List&amp;lt;T&amp;gt; to an ItemSource in a WPF control (e.g. ListView) then adding or updating entries in the&lt;br /&gt;List&amp;lt;T&amp;gt; does not cause the display to update (at least not without a bit of a hack to fool the UI into thinking that the&lt;br /&gt;DependencyProperty holding the List&amp;lt;T&amp;gt; has changed).&lt;br /&gt; &lt;br /&gt;
&lt;/div&gt;</description><author>hortha</author><pubDate>Mon, 05 May 2008 20:16:16 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080505P</guid></item></channel></rss>