WPF 依赖属性

本文详细介绍了WPF中依赖属性的定义与使用方法,包括如何为自定义控件添加及注册依赖属性,并提供了具体的代码实例。

依赖属性,简单的说,在WPF控件应用过程中,界面上直接可以引用的属性

如:<Button Content="aaa"></Button>

Content称为Button的依赖属性

当我们自定义控件时,如何添加依赖属性呢

1、添加属性

        /// <summary>
        /// get or set the items
        /// </summary>
        public List<TitleListItemModel> TitleListItems
        {
            get
            {
                return (List<TitleListItemModel>) GetValue(TitleListItemsProperty)
            }
            set{SetValue(TitleListItemsProperty,value);};
        }

2、注册属性

        public static readonly DependencyProperty TitleListItemsProperty = DependencyProperty.Register("TitleListItems", typeof(List<TitleListItemModel>),
            typeof(TitleListControl),new PropertyMetadata(new List<TitleListItemModel>()));

然后在应用自定义控件时,就能直接设置属性了,例如:

TitleListItems属性可以直接在界面上添加
        <wpfApplication6:TitleListControl VerticalAlignment="Center">
            <wpfApplication6:TitleListControl.TitleListItems>
                <wpfApplication6:TitleListItemModel Name="AAA" Text="aa"></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="bb" Text="BB"></wpfApplication6:TitleListItemModel>
                <wpfApplication6:TitleListItemModel Name="ccc" Text="CC"></wpfApplication6:TitleListItemModel>
            </wpfApplication6:TitleListControl.TitleListItems>
        </wpfApplication6:TitleListControl>

 

转载于:https://www.cnblogs.com/kybs0/p/5812174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值