天天看點

WPF xaml中清單依賴屬性的定義

清單内容屬性

WPF xaml中清單依賴屬性的定義

如上圖,是一個清單标題排序控件,我們需要定義一個标題清單,進而讓調用方可以自由的設定标題資訊。

在自定義控件時,會遇到清單依賴屬性,那麼該如何定義呢?

下面是錯誤的定義方式:

1     /// <summary>
 2     /// 辨別 <see cref="Headers"/> 的依賴項屬性。
 3     /// </summary>
 4     public static readonly DependencyProperty HeadersProperty = DependencyProperty.Register(
 5         "Headers", typeof(List<HeaderContent>), typeof(ListViewHeader),
 6         new PropertyMetadata(new List<HeaderContent>(), (d, e) => ((ListViewHeader)d).InitHeaderList()));
 7 
 8     /// <summary>
 9     /// 擷取或設定表頭的資訊集合。
10     /// 由于這是依賴項屬性,是以很難限制其不為 null,需要總是判空。
11     /// </summary>
12     public List<HeaderContent> Headers
13     {
14         get => (List<HeaderContent>)GetValue(HeadersProperty);
15         set => SetValue(HeadersProperty, value);
16     }      

按照如上依賴屬性的定義,

  • 必須提供一個預設屬性new List<HeaderContent>() 或者 在自定義控件初始化時設定預設清單值,不然界面調用此清單屬性去添加項,界面初始化時肯定會報錯~
  • 在Xaml中顯示時,不會報出一些錯誤提示資訊~(雖然不影響正常啟動,但是錯誤清單中一直顯示,對有強迫症的我來說。。不可忍受)

正确的實作方案

  • 定義清單依賴屬性:
1     /// <summary>
 2     /// 辨別 <see cref="Headers"/> 的依賴項屬性。
 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     /// 擷取或設定表頭的資訊集合。
10     /// 由于這是依賴項屬性,是以很難限制其不為 null,需要總是判空。
11     /// </summary>
12     public ListViewHeaderContentCollection Headers
13     {
14         get => (ListViewHeaderContentCollection)GetValue(HeadersProperty);
15         set => SetValue(HeadersProperty, value);
16     }      
  • 定義清單内容集合類:

  通過實作IList<T>和IList接口,可以讓清單在界面調用時,可以以清單的形式添加内容。

  注:将實作的接口方法修改下内容即可

1     public sealed class ListViewHeaderContentCollection : IList<HeaderContent>, IList
  2     {
  3         private readonly List<HeaderContent> _headContents = new List<HeaderContent>();
  4         public IEnumerator<HeaderContent> GetEnumerator()
  5         {
  6             return _headContents.GetEnumerator();
  7         }
  8 
  9         IEnumerator IEnumerable.GetEnumerator()
 10         {
 11             return GetEnumerator();
 12         }
 13 
 14         public void Add(HeaderContent item)
 15         {
 16             _headContents.Add(item);
 17         }
 18 
 19         public int Add(object value)
 20         {
 21             _headContents.Add((HeaderContent)value);
 22             return _headContents.Count;
 23         }
 24 
 25         public bool Contains(object value)
 26         {
 27             return _headContents.Contains((HeaderContent)value);
 28         }
 29 
 30         public void Clear()
 31         {
 32             _headContents.Clear();
 33         }
 34 
 35         public int IndexOf(object value)
 36         {
 37             return _headContents.IndexOf((HeaderContent)value);
 38         }
 39 
 40         public void Insert(int index, object value)
 41         {
 42             _headContents.Insert(index, (HeaderContent)value);
 43         }
 44 
 45         public void Remove(object value)
 46         {
 47             _headContents.Remove((HeaderContent)value);
 48         }
 49 
 50         void IList.RemoveAt(int index)
 51         {
 52             _headContents.RemoveAt(index);
 53         }
 54 
 55         object IList.this[int index]
 56         {
 57             get => _headContents[index];
 58             set => _headContents[index] = (HeaderContent)value;
 59         }
 60 
 61         public bool Contains(HeaderContent item)
 62         {
 63             return _headContents.Contains(item);
 64         }
 65 
 66         public void CopyTo(HeaderContent[] array, int arrayIndex)
 67         {
 68             _headContents.CopyTo(array, arrayIndex);
 69         }
 70 
 71         public bool Remove(HeaderContent item)
 72         {
 73             return _headContents.Remove(item);
 74         }
 75 
 76         public void CopyTo(Array array, int index)
 77         {
 78             _headContents.CopyTo((HeaderContent[])array, index);
 79         }
 80 
 81         public int Count => _headContents.Count;
 82 
 83         public object SyncRoot { get; }
 84 
 85         public bool IsSynchronized { get; }
 86 
 87         public bool IsReadOnly { get; }
 88 
 89         public bool IsFixedSize { get; }
 90 
 91         public int IndexOf(HeaderContent item)
 92         {
 93             return _headContents.IndexOf(item);
 94         }
 95 
 96         public void Insert(int index, HeaderContent item)
 97         {
 98             _headContents.Insert(index, item);
 99         }
100 
101         void IList<HeaderContent>.RemoveAt(int index)
102         {
103             _headContents.RemoveAt(index);
104         }
105 
106         public HeaderContent this[int index]
107         {
108             get => _headContents[index];
109             set => _headContents[index] = value;
110         }
111     }      

 調用:

WPF xaml中清單依賴屬性的定義

作者:

唐宋元明清2188

出處:

http://www.cnblogs.com/kybs0/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須在文章頁面給出原文連接配接,否則保留追究法律責任的權利。

下一篇: WPF 視窗