我有一个ObservableCollection和一个WPF UserControl是Databound.Control是一个图表,显示ObservableCollection中每个BarData类型项的垂直条.
ObservableCollection<BarData>
class BarData
{
public DateTime StartDate {get; set;}
public double MoneySpent {get; set;}
public double TotalMoneySpentTillThisBar {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
现在我想基于StartDate对ObservableCollection进行排序,以便BarData将在集合中按StartDate的顺序递增.然后我可以像这样计算每个BarData中TotalMoneySpentTillThisBar的值 -
var collection = new ObservableCollection<BarData>();
//add few BarData objects to collection
collection.Sort(bar => bar.StartData); // this is ideally the kind of function I was looking for which does not exist
double total = 0.0;
collection.ToList().ForEach(bar => {
bar.TotalMoneySpentTillThisBar = total + bar.MoneySpent;
total = bar.TotalMoneySpentTillThisBar;
}
);
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用ICollectionView对数据进行排序,过滤数据但不会改变实际的集合.我需要对实际集合进行排序,以便我可以为每个项目计算TotalMoneySpentTillThisBar.它的价值取决于集合中的项目顺序.
谢谢.
我正在寻找一个INotifyCollectionChanged的实施Stack和Queue.我可以自己滚动,但我不想重新发明轮子.
在ASP.Net专门工作了几年之后,我刚刚在WPF中弄湿了脚趾.我目前正在努力解决的问题是我有一个自定义集合类,我需要绑定到列表框.除了从集合中删除项目之外,一切似乎都在起作用.当我尝试得到错误时:问题“Collection Remove event must specify item position.” 是这个集合不使用索引,所以我没有看到指定位置的方法,到目前为止谷歌没有向我展示一个可行的解决方案......
该类被定义为实现ICollection<>和INotifyCollectionChanged.我的内部项容器是一个Dictionary使用项的名称(字符串)值的键.除了这两个接口定义的方法之外,此集合还有一个索引器,允许通过Name访问项目,并覆盖Contains和Remove方法,以便也可以使用项目Name调用它们.这适用于添加和编辑,但在我尝试删除时会抛出上述异常.
以下是相关代码的摘录:
class Foo
{
public string Name
{
get;
set;
}
}
class FooCollection : ICollection<Foo>, INotifyCollectionChanged
{
Dictionary<string, Foo> Items;
public FooCollection()
{
Items = new Dictionary<string, Foo>();
}
#region ICollection<Foo> Members
//***REMOVED FOR BREVITY***
public bool Remove(Foo item)
{
return this.Remove(item.Name);
}
public bool Remove(string name)
{
bool Value = this.Contains(name);
if (Value)
{
NotifyCollectionChangedEventArgs E = new …Run Code Online (Sandbox Code Playgroud)