分享Silverlight依赖属性的实现代码

昨天一位实习生在做项目时遇到依赖属性不知道如何实现,当时哥也蒙了一下.后来经过查找相关资料
找到了相应的方法,特将相关代码贴出来:

 [Category("Common")]
        public IEnumerable DataSource
        {
            get { return base.GetValue(DataSoureProperty) as IEnumerable; }
            set { base.SetValue(DataSoureProperty, value); }
        }

        static public readonly DependencyProperty DataSoureProperty = DependencyProperty.Register(
            "DataSource",
            typeof(IEnumerable),
            typeof(dependencyproperty),
            new PropertyMetadata(null, new PropertyChangedCallback(
                OnDataSourceChanged)));

        //public static readonly DependencyProperty DataSoureProperty = DependencyProperty.Register("Datasoure", typeof(IEnumerable), typeof(dependencyproperty), new PropertyMetadata(new PropertyChangedCallback(OnDataSourceChanged)));

         private static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            dependencyproperty ddd = (dependencyproperty)d;
            string oldValue = (string)e.OldValue;
            string newValue = (string)e.NewValue;
            RoutedPropertyChangedEventArgs<string> args = new RoutedPropertyChangedEventArgs<string>(oldValue, newValue);
            ddd.OnDataSourceChanged(args);
        }

        private  void  OnDataSourceChanged(RoutedPropertyChangedEventArgs<string> args)
        {
            this.DataBinding();
        }

   private void DataBinding()
        {
//Do somethings
        }

就以上的代码就可以实现一个DataSource依赖属性。

 

你可能感兴趣的:(silverlight)