Skip to main content

Microsoft Silverlight

Answered Question Can only bind ItemsSource to IEnumerable in XAMLRSS Feed

(0)

atm_grifter
atm_grifter

Member

Member

8 points

28 Posts

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. 

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

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.

atm_grifter
atm_grifter

Member

Member

8 points

28 Posts

Re: Can only bind ItemsSource to IEnumerable in XAML

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


 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities