Page view counter
Can only bind ItemsSource to IEnumerable in XAML Subscribe to this thread
Last post 07-25-2008 10:09 AM by atm_grifter. 2 replies.
Sort Posts:
07-23-2008 4:38 PM
Can only bind ItemsSource to IEnumerable in XAML

Since no one has responded to the following thread in the Controls forum, http://silverlight.net/forums/t/20873.aspx, I figured I'd go ahead and report this behavior as a bug.

To summarize in Silverlight you are not able to bind the ItemsSource of an ItemsControl to a collection that is any type other than IEnumerable, including types that implement IEnumerable.  So, you can't bind to List<T> or ObservableCollection<T>.  This seems like a glaring omission. 

atm_grifter

Loading...
Joined on 07-10-2008
Posts 28
07-25-2008 1:51 AM
Marked as Answer
Re: Can only bind ItemsSource to IEnumerable in XAML

Hello, thanks for reporting this issue. It looks similar to a known problem. TemplateBinding doesn't work for collection types. Note this only affects TemplateBinding. In a normal binding it should work fine. The data source can be any kind of collections. So a workaround is:

<ItemsControl x:Name="ic" Background="Blue" Margin="0,0,0,0"/>

 

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ItemsControl ic = this.GetTemplateChild("ic") as ItemsControl;
if (ic != null)
{
Binding b = new Binding("TestCollection");
b.Source = this;
ic.SetBinding(ItemsControl.ItemsSourceProperty, b);
}
}

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,747
07-25-2008 10:09 AM
Re: Can only bind ItemsSource to IEnumerable in XAML

 Yes, this is the workaround I've decided to use.  Thanks for the response!


 

atm_grifter

Loading...
Joined on 07-10-2008
Posts 28
Microsoft Communities