列表内容属性

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

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

下面是错误的定义方式:

     /// <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. linux下tomcat服务器的启动和关闭以及查看实时打印日志

    本页面中的操作都在tomcat的bin目录下 <一> 一般我都是使用: ./shutdom.sh //关闭tomcat ./startup.sh //开启tomcat服务 <二> ...

  2. 搭建一个舒适的 .NET Core 开发环境

    最近,一直在往.Net Core上迁移,随着工作的深入,发现.Net Core比.Net Framework好玩多了.不过目前还在windows下开发,虽然VisualStudio是宇宙第一神器,但是 ...

  3. Vue(day1)

    一.起步 <!-- 开发环境版本,包含了有帮助的命令行警告 --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/v ...

  4. [Swift]LeetCode198. 打家劫舍 | House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. [Swift]LeetCode276. 粉刷栅栏 $ Paint Fence

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  6. [Swift]LeetCode1029. 两地调度 | Two City Scheduling

    There are 2N people a company is planning to interview. The cost of flying the i-th person to city A ...

  7. Python—day18 dandom、shutil、shelve、系统标准流、logging

    一.dandom模块 (0, 1) 小数:random.random() [1, 10] 整数:random.randint(1, 10) [1, 10) 整数:random.randrange(1, ...

  8. Python内置函数(61)——str

    英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...

  9. 我曾做过陈士成,也做过孔乙己,还做过阿Q

    一. 我现在是陈士成,陈士成现在是我.为什么这么说呢? 那年那天,天刚微微亮,似乎还在打着哈欠.我和父亲去得很早,为的就是在“小升初的考试成绩榜单”前面占一个有利的位置.我不记得当时穿的厚还是不厚,体 ...

  10. ASP.NET Core 2.0 MVC - 获取当前登录用户信息

    一.前言 上篇实战完成后,没想到会有那么多的圈友给了那么多的支持,甚至连只是作为代码仓储的git上也给了一些小星星,真的感觉很惶恐啊,哈哈哈,毕竟代码写的很烂啊.由于上一篇只是大概说了下项目,所以准备 ...