Definition of List dependency property in WPF XAML

Source: Internet
Author: User

Original: Definition of List dependency property in WPF XAML

List Content Properties

For example, a list header sort control, we need to define a list of headings so that the caller is free to set the header information.

When you customize a control, you encounter list dependency properties, so how do you define it?

Here's how the error is defined:

1///<Summary>2Identity< Seecref= "Headers"/>'s dependency property. 3///</Summary>4 Public static readonly DependencyProperty headersproperty = Dependencyproperty.register (5"Headers", typeof (List<headercontent>), typeof (Listviewheader),6New PropertyMetadata (New List<headercontent>( ), (d, e) = = ((listviewheader) d). Initheaderlist ()));7 8///<Summary>9 ///Gets or sets the information collection for the header. Ten //// because this is a dependency property, it is difficult to limit its non-null and always empty.  One///</Summary> APublic List<headercontent>Headers -     { -Get = (List<headercontent>) GetValue (headersproperty); the set = = SetValue (headersproperty, value); -}

As defined by the dependency property above,

    • You must provide a default property of new list<headercontent>() or set the default list value when the custom control is initialized . Otherwise the interface calls this list property to add the item, the interface initialization will certainly error ~
    • When displayed in XAML, some error messages are not reported ~ (although not affecting normal startup, but the list of errors has always been shown, for me with obsessive-compulsive disorder). Not tolerable)
The right Implementation plan
    • To define a list dependency property:
1///<Summary>2Identity< Seecref= "Headers"/>'s dependency property. 3///</Summary>4 Public static readonly DependencyProperty headersproperty = Dependencyproperty.register (5 "Headers", typeof (Listviewheadercontentcollection), typeof (Listviewheader),6 New PropertyMetadata (Default (Listviewheadercontentcollection), (d, e) = = ((listviewheader) d). Initheaderlist ()));7 8///<Summary>9 ///Gets or sets the information collection for the header. Ten //// because this is a dependency property, it is difficult to limit its non-null and always empty.  One///</Summary> A Public listviewheadercontentcollection Headers -     { - get = (listviewheadercontentcollection) GetValue (headersproperty); the set = = SetValue (headersproperty, value); -}
    • To define a list content collection class:

By implementing the ilist<t> and IList interfaces, you can have the list add content as a list when the interface is called.

Note: The implementation of the interface method to modify the content can be

1public sealed class Listviewheadercontentcollection:ilist<headercontent>, IList2     {3Private ReadOnly List<headercontent>_headcontents = new List<headercontent>();4Public IEnumerator<headercontent>GetEnumerator ()5         {6 return _headcontents.getenumerator ();7         }8 9 IEnumerator ienumerable.getenumerator ()Ten         { One return GetEnumerator (); A         } -  - Public void Add (headercontent item) the         { - _headcontents.add (item); -         } -  + public int Add (object value) -         { + _headcontents.add ((headercontent) value); A return _headcontents.count; at         } -  - Public bool Contains (object value) -         { - return _headcontents.contains ((headercontent) value); -         } in  - Public void Clear () to         { + _headcontents.clear (); -         } the  * public int IndexOf (object value) $         {Panax Notoginseng return _headcontents.indexof ((headercontent) value); -         } the  + Public void Insert (int index, object value) A         { the _headcontents.insert (index, (headercontent) value); +         } -  $ Public void Remove (object value) $         { - _headcontents.remove ((headercontent) value); -         } the  - void ilist.removeat (int index)Wuyi         { the _headcontents.removeat (index); -         } Wu  - Object Ilist.this[int Index] About         { $ get = _headcontents[index]; - set = = _headcontents[index] = (headercontent) value; -         } -  A Public bool Contains (headercontent Item) +         { the return _headcontents.contains (item); -         } $  the Public void CopyTo (headercontent[] array, int arrayindex) the         { the _headcontents.copyto (array, arrayindex); the         } -  in Public bool Remove (headercontent Item) the         { the return _headcontents.remove (item); About         } the  the Public void CopyTo (array array, int index) the         { + _headcontents.copyto ((headercontent[]) array, index); -         } the Bayi public int Count = _headcontents.count; the  the Public object SyncRoot {get;} -  - Public bool issynchronized {get;} the  the Public bool IsReadOnly {get;} the  the Public bool Isfixedsize {get;} -  the public int IndexOf (headercontent item) the         { the return _headcontents.indexof (item);94         } the  the Public void Insert (int index, headercontent item) the         {98 _headcontents.insert (index, item); About         } - 101void IList<headercontent>. RemoveAt (int index)102         {103 _headcontents.removeat (index);104         } the 106 Public headercontent This[int index]107         {108 get = _headcontents[index];109 set = = _headcontents[index] = value; the         }111}

Call:

Definition of List dependency property in WPF XAML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.