WPF自定义控件

1.自定义属性,项WPF属性系统注册的依赖项属性。如:

 //为自定义控件注册一个属性,ImageSource来作为填充在按钮中的图片
     public static  readonly DependencyProperty  ImageSourceProperty;
 

  2. 属性注册

  属性注册可在属性定义时注册,也可在静态构造函数中注册。

 ImageSourceProperty   = DependencyProperty.Register("ImageSource",
typeof(BitmapSource),typeof(UButton),  new FrameworkPropertyMetadata(
                       new PropertyChangedCallback(OnSourceImageChange)));
 

  3. 属性回调函数

 /// <summary>
        /// 常规属性ImageSource
        /// </summary>
        public BitmapSource ImageSource
        {
           get
            {
                return (BitmapSource)GetValue(ImageSourceProperty);
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

  4. XAML中使用自定义控件

 <g:UButton ImageSource="image/3.png" Margin="200,400,0,0" TextContent="画图板" ></g:UButton>

posted on 2013-03-21 11:45  AI_JJ  阅读(303)  评论(0)    收藏  举报

导航