How to make the Silverlight Custom Control property data-bound?

I created a custom silverlight control consisting of two date pickers and a combobox. I would like to do combobox data binding, and I know that I need to use DependencyProperty. What I'm not sure is exactly how to build it. Here is the code I have:

#region ItemsSource (DependencyProperty)

    /// <summary>
    /// ItemsSource to bind to the ComboBox
    /// </summary>
    public IList ItemsSource
    {
        get { return (IList)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(int), typeof(DateRangeControl),
          new PropertyMetadata(0));

    #endregion

The problem is that all the patterns I've seen are for simple properties like Text or Background that expect either a string, int, or color. Since I'm trying to bind to a combobox ItemsSource element, it expects IEnumerable, I did not know how to build a property for this. I used IList.

Can someone please tell me if I'm on the right track and give me some pointers? Thanks

+3
2

, . , DP, . , typeof (int) typeof (IList).

, . , , IEnumerable, , IList.

+3

?

        public IEnumerable ItemsSource
    {
        get
        {
            return (IEnumerable)GetValue(ItemsSourceProperty);
        }
        set
        {
            SetValue(ItemsSourceProperty, value);
        }
    }

    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(DateRangeControl), new PropertyMetadata(null));

IEnumerable System.Collections.Generic

+1

Source: https://habr.com/ru/post/1709307/


All Articles