问题:当前ListBox Items 绑定 集合数据源ListA时候;ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变?

实体类继承 INotifyCollectionChanged 即可实现:

BaseViewModel:

public class BaseViewModel : INotifyPropertyChanged, INotifyCollectionChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
public event NotifyCollectionChangedEventHandler CollectionChanged; public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} public virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
} public void Dispose()
{
this.OnDispose();
} protected void OnDispose() { } public void OnCollectionChanged()
{
}
}

ViewModel:

public class ViewModel : BaseViewModel
{
private List<Person> _lp = null;
private RelayCommand _addCommand, _removeCommand; public ViewModel()
{
LP = new List<Person>();
LP.Add(new Person(, "aaa"));
LP.Add(new Person(, "bbb"));
} public List<Person> LP
{
get { return _lp; }
set { _lp = value; }
} public ICommand AddCommand
{
get
{
if (_addCommand == null)
{ _addCommand = new RelayCommand(param => this.Add(), param => this.CanAdd); }
return _addCommand;
}
} public bool CanAdd
{ get { return true; } } public void Add()
{
Person ps = new Person(, "ccc");
LP.Add(ps);
CollectionChanged += new NotifyCollectionChangedEventHandler(List_CollectionChanged);
OnPropertyChanged("LP");
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, LP[], ));
} public ICommand RemoveCommand
{
get
{
if (_removeCommand == null)
{ _removeCommand = new RelayCommand(param => this.Remove(), param => this.CanRemove); }
return _removeCommand;
}
} public bool CanRemove
{ get { return true; } } public void Remove()
{
LP.RemoveAt();
CollectionChanged += new NotifyCollectionChangedEventHandler(List_CollectionChanged);
OnPropertyChanged("LP");
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, LP[], ));
} private void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// ??????????
}
}

完美解决!!

https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/1b55492b-e9ca-4610-a40d-107d64c8ea9f/inotifycollectionchanged-on-listt

WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged的更多相关文章

  1. WPF绑定到集合

    什么是集合视图? 集合视图是位于绑定源集合顶部的一层,您可以通过它使用排序.筛选和分组查询来导航和显示源集合,而无需更改基础源集合本身.集合视图还维护着一个指向集合中的当前项的指针.如果源集合实现了 ...

  2. WPF 绑定以基础数据类型为集合的无字段名的数据源

    WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...

  3. 分割list,将集合按规定个数分为n个部分。

    /** * 按指定大小,分隔集合,将集合按规定个数分为n个部分 * * @param list * @param len * @return */ public static <T> Li ...

  4. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  5. poj 1611 求0号结点所在集合的元素个数

    求0号结点所在集合的元素个数 Sample Input 100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0Sample Out ...

  6. WPF快速入门系列(4)——深入解析WPF绑定

    一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...

  7. 【转】【WPF】WPF绑定用法

    一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...

  8. WPF - 绑定及惯用法(一)

    写在前面:这仍然是一些没有经过严格审阅的文字.虽然我的确执行了初稿.复稿以及审阅等一系列用以保证文章质量的方法,但是仍然担心其中是否有错误.希望您能帮助指出,以在下一次我在版本更新时进行修正.所有的错 ...

  9. wpf绑定全局静态变量(mvvm)

    原文 wpf绑定全局静态变量(mvvm) 在实际的开发中,有一些集合或者属性可能是全局的,比如当你做一个oa的时候,可能需要展示所有的人员,这时这个所有的人员列表显然可以作为全局参数,比如这里有一个全 ...

随机推荐

  1. springboot Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    报错如下: 在请求目标中发现无效字符.有效字符在RFC 7230和RFC 3986中定义. 原因是Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证. 就是 ...

  2. printf固定一行打印倒计时的实现

    @2019-07-15 [小记] #include<stdlib.h> #include <stdio.h> #include <time.h> #include ...

  3. mysql存储json

    1. json_merge 合并Json并返回 update `user` set inviteeMap = json_merge(inviteeMap, '{"xx1":100} ...

  4. centos 最小化安装pycharm

    首先找到安装地址 https://www.jetbrains.com/pycharm/download/#section=linux 选择linux. 打开浏览器的network监视,我用的chrom ...

  5. 2.Locust 跑起来试试

    代码 from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): @task def baidu(self): ...

  6. flutter flutter_cupertino_date_picker 时间插件的用法

    https://blog.csdn.net/sinat_37255207/article/details/100041023 https://github.com/wuzhendev/flutter- ...

  7. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) D. Delivery Delays (二分+最短路+DP)

    题目链接:https://codeforc.es/gym/101933/problem/D 题意:地图上有 n 个位置和 m 条边,每条边连接 u.v 且有一个距离 w,一共有 k 个询问,每个询问表 ...

  8. 爬当当网上python书籍的图片

    1.分析网页代码,获取图片下载连接:http://img3m4.ddimg.cn/20/11/23473514-1_b_5.jpg 2. python实现代码 import os import re ...

  9. KindEditor完全复制word内容

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...

  10. java new一个对象的过程中发生了什么

    java在new一个对象的时候,会先查看对象所属的类有没有被加载到内存,如果没有的话,就会先通过类的全限定名来加载.加载并初始化类完成后,再进行对象的创建工作. 我们先假设是第一次使用该类,这样的话n ...