列表内容属性

如上图,是一个列表标题排序控件,我们需要定义一个标题列表,从而让调用方可以自由的设置标题信息。

在自定义控件时,会遇到列表依赖属性,那么该如何定义呢?

下面是错误的定义方式:

     /// <summary>
/// 标识 <see cref="Headers"/> 的依赖项属性。
/// </summary>
public static readonly DependencyProperty HeadersProperty = DependencyProperty.Register(
"Headers", typeof(List<HeaderContent>), typeof(ListViewHeader),
new PropertyMetadata(new List<HeaderContent>(), (d, e) => ((ListViewHeader)d).InitHeaderList())); /// <summary>
/// 获取或设置表头的信息集合。
/// 由于这是依赖项属性,所以很难限制其不为 null,需要总是判空。
/// </summary>
public List<HeaderContent> Headers
{
get => (List<HeaderContent>)GetValue(HeadersProperty);
set => SetValue(HeadersProperty, value);
}

按照如上依赖属性的定义,

  • 必须提供一个默认属性new List<HeaderContent>() 或者 在自定义控件初始化时设置默认列表值,不然界面调用此列表属性去添加项,界面初始化时肯定会报错~
  • 在Xaml中显示时,不会报出一些错误提示信息~(虽然不影响正常启动,但是错误列表中一直显示,对有强迫症的我来说。。不可忍受)

正确的实现方案

  • 定义列表依赖属性:
     /// <summary>
/// 标识 <see cref="Headers"/> 的依赖项属性。
/// </summary>
public static readonly DependencyProperty HeadersProperty = DependencyProperty.Register(
"Headers", typeof(ListViewHeaderContentCollection), typeof(ListViewHeader),
new PropertyMetadata(default(ListViewHeaderContentCollection), (d, e) => ((ListViewHeader)d).InitHeaderList())); /// <summary>
/// 获取或设置表头的信息集合。
/// 由于这是依赖项属性,所以很难限制其不为 null,需要总是判空。
/// </summary>
public ListViewHeaderContentCollection Headers
{
get => (ListViewHeaderContentCollection)GetValue(HeadersProperty);
set => SetValue(HeadersProperty, value);
}
  • 定义列表内容集合类:

  通过实现IList<T>和IList接口,可以让列表在界面调用时,可以以列表的形式添加内容。

  注:将实现的接口方法修改下内容即可

     public sealed class ListViewHeaderContentCollection : IList<HeaderContent>, IList
{
private readonly List<HeaderContent> _headContents = new List<HeaderContent>();
public IEnumerator<HeaderContent> GetEnumerator()
{
return _headContents.GetEnumerator();
} IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
} public void Add(HeaderContent item)
{
_headContents.Add(item);
} public int Add(object value)
{
_headContents.Add((HeaderContent)value);
return _headContents.Count;
} public bool Contains(object value)
{
return _headContents.Contains((HeaderContent)value);
} public void Clear()
{
_headContents.Clear();
} public int IndexOf(object value)
{
return _headContents.IndexOf((HeaderContent)value);
} public void Insert(int index, object value)
{
_headContents.Insert(index, (HeaderContent)value);
} public void Remove(object value)
{
_headContents.Remove((HeaderContent)value);
} void IList.RemoveAt(int index)
{
_headContents.RemoveAt(index);
} object IList.this[int index]
{
get => _headContents[index];
set => _headContents[index] = (HeaderContent)value;
} public bool Contains(HeaderContent item)
{
return _headContents.Contains(item);
} public void CopyTo(HeaderContent[] array, int arrayIndex)
{
_headContents.CopyTo(array, arrayIndex);
} public bool Remove(HeaderContent item)
{
return _headContents.Remove(item);
} public void CopyTo(Array array, int index)
{
_headContents.CopyTo((HeaderContent[])array, index);
} public int Count => _headContents.Count; public object SyncRoot { get; } public bool IsSynchronized { get; } public bool IsReadOnly { get; } public bool IsFixedSize { get; } public int IndexOf(HeaderContent item)
{
return _headContents.IndexOf(item);
} public void Insert(int index, HeaderContent item)
{
_headContents.Insert(index, item);
} void IList<HeaderContent>.RemoveAt(int index)
{
_headContents.RemoveAt(index);
} public HeaderContent this[int index]
{
get => _headContents[index];
set => _headContents[index] = value;
}
}

调用:

WPF xaml中列表依赖属性的定义的更多相关文章

  1. xaml中的依赖属性

    wpf使用依赖属性完成数据绑定.动画.属性变更通知.样式化等.对于数据绑定.绑定到.NET属性源上的UI元素的属性必须是依赖属性 .net的一般属性定义如下 private int val;      ...

  2. WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性

    原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...

  3. WPF中的依赖属性

    1. WPF中的依赖属性 依赖属性是专门基于WPF创建的.在WPF库实现中,依赖属性使用普通的C#属性进行了包装,使用方法与普通的属性是相同的. 1.1 依赖属性提供的属性功能 资源 数据绑定 样式 ...

  4. WPF ----在UserControl的xaml里绑定依赖属性

    场景:在定义wpf 用户控件的时候,希望使用时设置自定义的属性来改变用户控件里的状态或内容等. 下面直接上实例代码: 用户控件的后台代码,定义依赖属性 public partial class MyU ...

  5. WPF学习笔记一 依赖属性及其数据绑定

    本文想通过由浅入深的讲解让读者比较深的理解依赖属性.  首先,我们回顾一下依赖属性的发展历史. 最初,人们提出面向对象编程时,并没有属性这个说法,当时叫做成员变量.一个对象由成员变量和成员函数组成,如 ...

  6. WPF学习笔记二 依赖属性实现原理及性能分析

    在这里讨论依赖属性实现原理,目的只是学习WPF是怎么设计依赖属性的,同时更好的使用依赖属性. 首先我们来思考一个简单的问题:我们希望能验证属性的值是否有效,属性变更时进行自己的处理.回顾一下.net的 ...

  7. ReferentialConstraint 中的依赖属性映射到由存储生成的列

    ReferentialConstraint 中的依赖属性映射到由存储生成的列 这个问题是由于从表中的外键关系建立错误(可能是由于误改),查看从表的所有外键关系,即可找到问题所在. 问题: 什么是从表? ...

  8. Entity Framework问题:ReferentialConstraint 中的依赖属性映射由存储生成的列

    原文:Entity Framework问题:ReferentialConstraint 中的依赖属性映射由存储生成的列 今天在采用Entity Framework 的Database First反向以 ...

  9. (原创)2. WPF中的依赖属性之二

    1 依赖属性 1.1 依赖属性最终值的选用 WPF属性系统对依赖属性操作的基本步骤如下: 第一,确定Base Value,对同一个属性的赋值可能发生在很多地方.还用Button的宽度来进行举例,可能在 ...

随机推荐

  1. 关于实体类getset方法首字母小写问题

    实体类:private Date cDateTime;private String cNickname; public Date getcDateTime() { return cDateTime;} ...

  2. RHEL7 配置网络yum源

    redhat系统安装好尽管默认带有yum,但是redhat的更新包只对注册用户有效(收费).所以需要更换yum源. 基本的流程就是: 1.删除redhat7.0系统自带的yum软件包: 2.自行下载所 ...

  3. ubuntu14.04 安装lnmp + redis

    1.更新源 apt-get install update 2.安装nginx : apt-get install nginx 配置nginx: ① cd /etc/sites-enabled/ ② v ...

  4. python3 stack/ queue和deque模块

    '''栈stack 先进后出FILO (first in last out)'''lst = []lst.append("张一山")lst.append("杨紫" ...

  5. github pages + Hexo + node.js 搭建属于自己的个人博客网站

     之前我写过一篇用Github实现个人主页的博客:https://www.cnblogs.com/tu-0718/p/8081288.html   后来看到某个大佬写的文章:[5分钟 0元搭建个人独立 ...

  6. [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  7. [Swift]LeetCode274.H指数 | H-Index

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  8. [Swift]LeetCode437. 路径总和 III | Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [Swift]LeetCode672. 灯泡开关 Ⅱ | Bulb Switcher II

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

  10. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...