当前位置:编程学习 > wap >>

Windows phone 7之依赖属性(Dependency Properties)

依赖属性(Dependency Properties)
如果你要创建一个自定义控件类,并为这个类定义新的属性,如果你想给这些属性设置Style,或者你想通过给那些属性设置数据绑定(Binding),或者想对这些属性使用动画,那么你需要将这些属性设置成依赖属性(Dependency Properties)。
下面我们先看一个简单了例子,自定义一个类,继承自Button,也就是我们自己做一个Button,并为之定义两个属性Color1和Color2,代码如下
public class MyButton : Button
    {
        public MyButton()
        {
           
        }

        private Color _forceColor;
        public Color ForceColor
        {
            get
            {
                return _forceColor;
            }
            set
            {
                _forceColor = value;
                this.Foreground = new SolidColorBrush() { Color = _forceColor};
            }
        }

        private Color _backColor;
        public Color BackColor
        {
            get
            {
                return _backColor;
            }
            set
            {
                _backColor = value;
                this.Background = new SolidColorBrush() { Color = _backColor };
            }
        }
    }
上面定义一个Button,声明两个属性Color1和Color2,为Color1属性Set值时,同时将值赋给前景色Foreground,为Color2属性Set值时,同时将值赋给背景色Background,
在MainPage中添当前项目的加命名空间xmlns:local="clr-namespace:DependencyProperties"
在页面中添加一个MyButton,直接枚举设置ForceColor和BackColor:
<local:MyButton BackColor="Red" ForceColor="Black" Content="MyButton1"></local:MyButton>
添加资源
<Color x:Key="MyForceColor">Red</Color>
<Color x:Key="MyBackColor">White</Color>
再在页面中添加一个MyButton,设置ForceColor和BackColor值为上面定义的资源
<local:MyButton BackColor="{StaticResource MyBackColor}" ForceColor="{StaticResource MyForceColor}" Content="MyButton2"></local:MyButton>
现在运行一下程序,效果如下


 
上面是预料中的结果,现在我们用Style设置ForceColor和BackColor的值,定义资源

上面是我定义资源时,代码编辑器中的效果,看到有错误提示,看看错误窗口

未将对象引用设置到对象的实例,这事怎么回事呢,先不管他,
在页面中添加一个MyButton,并使用上面定义的Style
<local:MyButton Style="{StaticResource MyStyle}" Content="MyButton3"></local:MyButton>
运行一下,看看

调试也是通不过,为什么呢,这就是我之前说的,要想为自定义的属性使用Style,那么就必须将之设置为DependencyProperty
DependencyProperty的定义格式为
 
 

 

public static readonly DependencyProperty 变量名=
DependencyProperty.Register("属性名",
typeof(属性类型),
typeof(所属类的类型),
new PropertyMetadata(默认值, 值变化时触发的方法));
现在我们修改之前的代码,将ForceColor和BackColor设置为DependencyProperty,修改后的MyButton类如下
public class MyButton : Button
    {
        public MyButton()
        {

        }

        public static readonly DependencyProperty ForceColorProperty =
            DependencyProperty.Register("ForceColor",
            typeof(Color),
            typeof(MyButton),
            new PropertyMetadata(Colors.Black, OnColorChanged));

        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor",
            typeof(Color),
            typeof(MyButton),
            new PropertyMetadata(Colors.White, OnColorChanged));

        public Color ForceColor
        {
            set { SetValue(ForceColorProperty, value); }
            get { return (Color)GetValue(ForceColorProperty); }
        }
        public Color BackColor
        {
            set { SetValue(BackColorProperty, value); }
            get { return (Color)GetValue(BackColorProperty); }
        }

        static void OnColorChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs args)
        {
            var btn = obj as MyButton;
            if (args.Property == ForceColorProperty)
            {
                btn.Foreground = new SolidColorBrush() { Color = (Color)args.NewValue };
            }
 &nbs

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,