110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtendList
{
public delegate void ListChangedEventHandler<T>(object sender);
public class MyList<T> : List<T>
{
private int _count;
public String Action { get; set; }
private int MCount
{
get { return _count; }
set
{
if (_count != value)
{
_count = this.Count;
DoListChangedEvent();
}
}
}
public event ListChangedEventHandler<T> ListChanged;
private void DoListChangedEvent()
{
if (this.ListChanged != null)
this.ListChanged(this);
}
public new void Add(T t)
{
base.Add(t);
this.Action = "Add";
MCount++;
}
public new void Remove(T t)
{
base.Remove(t);
this.Action = "Remove";
MCount--;
}
}
class Program
{
static void Main(string[] args)
{
MyList<int> mylist = new MyList<int>();
mylist.ListChanged += List_CollectionChanged<int>;
mylist.ListChanged += List_SummaryChanged<int>;
mylist.Add(1);
mylist.Add(2);
mylist.Remove(1);
Console.ReadKey();
}
static void List_CollectionChanged<T>(object sender)
{
Console.Write("{0} an item. Current items: {1} \n", ((MyList<T>)sender).Action, String.Join(" ", (MyList<T>)sender));
}
static void List_SummaryChanged<T>(object sender)
{
Console.Write("*** {0} an item. Current summary: {1} \n", ((MyList<T>)sender).Action, ((MyList<T>)sender).Sum(item => int.Parse(item.ToString())));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtendList
{
public delegate void ListChangedEventHandler<T>(object sender);
public class MyList<T> : List<T>
{
private int _count;
public String Action { get; set; }
private int MCount
{
get { return _count; }
set
{
if (_count != value)
{
_count = this.Count;
DoListChangedEvent();
}
}
}
public event ListChangedEventHandler<T> ListChanged;
private void DoListChangedEvent()
{
if (this.ListChanged != null)
this.ListChanged(this);
}
public new void Add(T t)
{
base.Add(t);
this.Action = "Add";
MCount++;
}
public new void Remove(T t)
{
base.Remove(t);
this.Action = "Remove";
MCount--;
}
}
class Program
{
static void Main(string[] args)
{
MyList<int> mylist = new MyList<int>();
mylist.ListChanged += List_CollectionChanged<int>;
mylist.ListChanged += List_SummaryChanged<int>;
mylist.Add(1);
mylist.Add(2);
mylist.Remove(1);
Console.ReadKey();
}
static void List_CollectionChanged<T>(object sender)
{
Console.Write("{0} an item. Current items: {1} \n", ((MyList<T>)sender).Action, String.Join(" ", (MyList<T>)sender));
}
static void List_SummaryChanged<T>(object sender)
{
Console.Write("*** {0} an item. Current summary: {1} \n", ((MyList<T>)sender).Action, ((MyList<T>)sender).Sum(item => int.Parse(item.ToString())));
}
}
}
[/quote]
感谢您的耐心回答,我也学习到了很多管理List<T>的知识
问题已经搞定了,通过对比,我发现监控List<T>的代价相对有点高,还是直接在TextBox层解决了,通过其他方法遍历出了所有的TextBox,为所有的TextBox绑定了事件,问题圆满解决。
再次感谢!static void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (DataType obj in e.NewItems)
obj.PropertyChanged += obj_PropertyChanged;
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace ConsoleApplication1
{
class Program
{
private static ObservableCollection<DataType> List = new ObservableCollection<DataType>();
static void Main(string[] args)
{
List.CollectionChanged += List_CollectionChanged;
List.Add(new DataType { Fa = "aaa", Fb = 1 });
List.Add(new DataType { Fa = "bbb", Fb = 2 });
List.Add(new DataType { Fa = "ccc", Fb = 3 });
List.Add(new DataType { Fa = "ddd", Fb = 4 });
List[3].Fb += 100; //这个对象修改时,可以在方法obj_PropertyChanged中捕获。
Console.ReadKey();
}
static void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
var obj = (DataType)e.NewItems[0];
obj.PropertyChanged += obj_PropertyChanged;
}
}
static void obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var obj = (DataType)sender;
Console.WriteLine("Fa={0}, Fb={1}", obj.Fa, obj.Fb);
}
}
public class DataType : INotifyPropertyChanged
{
private string _Fa;
public string Fa
{
get { return _Fa; }
set
{
if (_Fa != value)
{
_Fa = value;
RaisePropertyChangedEvent("Fa");
}
}
}
private int _Fb;
public int Fb
{
get { return _Fb; }
set
{
if (_Fb != value)
{
_Fb = value;
RaisePropertyChangedEvent("Fb");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChangedEvent(string name)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
private Control[] goods_HsCode = new Control[2];
/// <summary>
/// HS编码
/// </summary>
public Control[] Goods_HsCode
{
get { return goods_HsCode; }
set { goods_HsCode = value; }
}
//在你的窗体加载事件中 给控件类的属性赋值
customsInspectionControl.Goods_HsCode[0] = lblGoods_HsCode;
customsInspectionControl.Goods_HsCode[1] = txtGoods_HsCode;//HS编码
/// <summary>
/// 此方法用于在弹出商品种类的时候调用中调用,
/// 防止用户改变窗体上文本框的值时我不能及时获取,只有调用此方法才获取修改后的值
/// 因为商品的值是通过list集合穿来的,而修改只是修改控件属性的值,调用此方法把控件的值赋给list集合对应的值
/// </summary>
/// <param name="customsInspectionControl"></param>
public static void GoodsUpdateValue(CustomsInspectionControl customsInspectionControl)
{
if (customsInspectionControl.ListQuery_GoodsInfoInspectionFJ.Count > 0)
{
customsInspectionControl.ListQuery_GoodsInfoInspectionFJ[customsInspectionControl.GoodsPage].MillWorkPlant = customsInspectionControl.MillworkPlant[1].Text;
customsInspectionControl.ListQuery_GoodsInfoInspectionFJ[customsInspectionControl.GoodsPage].Goods_HsCode = customsInspectionControl.Goods_HsCode[1].Text;
}
}
一个存储 你的lable 另一个存储 text