C# 简单状态消息实现
我试图创建一个基本类,它允许不同的控件绑定并显示一些值 我想要一个静态的对象列表,其中每个对象都有一些属性,比如标题、记号计数器等等 然后我想将标签绑定到最后添加到这个列表和datagridview中的项,以允许查看它们 如果这种解决方案同时适用于winforms和wpf环境,那就太好了 如果你能告诉我我做错了什么。谢谢 下面是想法草案(其中一个已经通过测试,但失败了) 身份等级:C# 简单状态消息实现,c#,wpf,winforms,class,properties,C#,Wpf,Winforms,Class,Properties,我试图创建一个基本类,它允许不同的控件绑定并显示一些值 我想要一个静态的对象列表,其中每个对象都有一些属性,比如标题、记号计数器等等 然后我想将标签绑定到最后添加到这个列表和datagridview中的项,以允许查看它们 如果这种解决方案同时适用于winforms和wpf环境,那就太好了 如果你能告诉我我做错了什么。谢谢 下面是想法草案(其中一个已经通过测试,但失败了) 身份等级: public class Status: INotifyPropertyChanged { public
public class Status: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
//implementation of observable collection
private static ObservableCollection<Status> _list;
public static ObservableCollection<Status> List
{
get { return _list ?? (_list = new ObservableCollection<Status>{new Status()}); }
}
//object properties
public string Message { get; set; }
public bool Finished { get; set; }
//object views
public string View
{
get { return Message + "(" + Finished + ")" ; }
}
//object methods
public static Status Add(string message)
{
var result = new Status
{
Message = message,
Finished = false
};
List.Add(result);
return result;
}
public void Finish()
{
Finished = true;
}
}
这就成功了:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using WindowsFormsApplication2.Annotations;
namespace WindowsFormsApplication2
{
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
#region Notyfier implementation
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region collection implemetation
public BindingList<Item> Items = new BindingList<Item>();
public string Count
{
get { return (Items.Count == 1)
? "1 item."
: Items.Count + " items."; }
}
public Item Current
{
get { return Items.Count == 0
? new Item {Colour = Color.Chartreuse} //default initial item
: Items.Last(); }
}
#endregion
#region object implemetation
protected object ID { get; set; }
public Color Colour { get; set; }
public void NewItem(Color color)
{
Items.Add(new Item
{
ID = Guid.NewGuid(),
Colour = color
});
OnPropertyChanged("Count");
OnPropertyChanged("Current");
}
#endregion
}
}
使用系统;
使用系统组件模型;
使用系统图;
使用System.Linq;
使用System.Runtime.CompilerServices;
使用WindowsFormsApplication2.注释;
命名空间Windows窗体应用程序2
{
公共类项目:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
#诺蒂菲尔地区实施
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
#端区
#区域集合实现
public BindingList Items=new BindingList();
公共字符串计数
{
获取{return(Items.Count==1)
“1项。”
:Items.Count+“Items.”;}
}
公共项目当前
{
获取{返回项。计数==0
?新项目{Color=Color.chartrese}//默认初始项目
:Items.Last();}
}
#端区
#区域对象实现
受保护对象ID{get;set;}
公共颜色{get;set;}
public void NewItem(颜色)
{
项目。添加(新项目)
{
ID=Guid.NewGuid(),
颜色=颜色
});
不动产变更(“计数”);
不动产变更(“当前”);
}
#端区
}
}
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using WindowsFormsApplication2.Annotations;
namespace WindowsFormsApplication2
{
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
#region Notyfier implementation
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region collection implemetation
public BindingList<Item> Items = new BindingList<Item>();
public string Count
{
get { return (Items.Count == 1)
? "1 item."
: Items.Count + " items."; }
}
public Item Current
{
get { return Items.Count == 0
? new Item {Colour = Color.Chartreuse} //default initial item
: Items.Last(); }
}
#endregion
#region object implemetation
protected object ID { get; set; }
public Color Colour { get; set; }
public void NewItem(Color color)
{
Items.Add(new Item
{
ID = Guid.NewGuid(),
Colour = color
});
OnPropertyChanged("Count");
OnPropertyChanged("Current");
}
#endregion
}
}