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