Skip to main content

Microsoft Silverlight

Binding ItemsSource to custom collection failsRSS Feed

(0)

atm_grifter
atm_grifter

Member

Member

8 points

28 Posts

Binding ItemsSource to custom collection fails

 The following works in WPF, and everything I've read leads me to believe this should work in Silverlight as well:

        <controls:TestControl>
            <controls:TestControl.Template>
                <ControlTemplate TargetType="controls:TestControl">
                    <Grid>
                        <ItemsControl Margin="0,0,0,0" ItemsSource="{TemplateBinding TestCollection}">
                        </ItemsControl>
                    </Grid>
                </ControlTemplate>
            </controls:TestControl.Template>
            <controls:TestControl.TestCollection>
                <Slider/>
            </controls:TestControl.TestCollection>
        </controls:TestControl>
 

    public class TestControl : Control, INotifyPropertyChanged
    {
        public static readonly DependencyProperty TestCollectionProperty = DependencyProperty.Register("TestCollection", typeof(ObservableCollection<Slider>), typeof(TestControl), null);
        public event PropertyChangedEventHandler PropertyChanged;

        public TestControl()
        {
            SetValue(TestCollectionProperty, new ObservableCollection<Slider>());
         }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
       }

        public ObservableCollection<Slider> TestCollection
        {
            get
            {
                return (ObservableCollection<Slider>)GetValue(TestCollectionProperty);
            }
            set
            {
                SetValue(TestCollectionProperty, value);
                OnPropertyChanged("TestCollection");
            }
        }
    }
 

I'm not sure if the INotifyPropertyChanged stuff is necessary (this is still a point of great confusion for me), but I doubt it would be responsible for it not working, and even when I don't implement that interface it still doesn't work (which is to say, nothing gets rendered).  In the WPF version, I don't implement INotifyPropertyChanged, and it works as expected (which is to say, you see a slider at the top of the window). 

atm_grifter
atm_grifter

Member

Member

8 points

28 Posts

Re: Binding ItemsSource to custom collection fails

 So, after more playing with how to accomplish what I'm trying to do I've discovered a few things (with some co-workers help).  If I name my ItemsControl in the template and harvest it in the code-behind file I can set it's ItemsSource directly to my collection and it will work as expected.  Similarly, I can create a static resource of a strongly-typed collection (ala SliderCollection : ObservableCollection<Slider>) in my XAML resources and set the ItemsSource directly to that resource.

For the TemplateBinding to function, apparently the type of the collection has to be IEnumerable, not implement IEnumerable, but actually be of that type.  This is absolutely broken. There is no reason I should have to name my parts in a template in order for me to perform a binding, nor should I be forced to make my public interface to my control so generic that you can set it's collection(s) to anything. I want strongly-typed collections as part of my control's public interface that I can bind an ItemsControl to in my control's template in XAML.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities