相关疑难解决方法(0)

如何对ObservableCollection进行排序

我有一个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.它的价值取决于集合中的项目顺序.

谢谢.

sorting wpf observablecollection icollectionview

16
推荐指数
3
解决办法
3万
查看次数

可观察的堆栈和队列

我正在寻找一个INotifyCollectionChanged的实施StackQueue.我可以自己滚动,但我不想重新发明轮子.

c# queue stack inotifycollectionchanged

15
推荐指数
3
解决办法
2万
查看次数

在没有索引的集合上实现INotifyCollectionChanged

在ASP.Net专门工作了几年之后,我刚刚在WPF中弄湿了脚趾.我目前正在努力解决的问题是我有一个自定义集合类,我需要绑定到列表框.除了从集合中删除项目之外,一切似乎都在起作用.当我尝试得到错误时:问题“Collection Remove event must specify item position.” 是这个集合不使用索引,所以我没有看到指定位置的方法,到目前为止谷歌没有向我展示一个可行的解决方案......

该类被定义为实现ICollection<>INotifyCollectionChanged.我的内部项容器是一个Dictionary使用项的名称(字符串)值的键.除了这两个接口定义的方法之外,此集合还有一个索引器,允许通过Name访问项目,并覆盖ContainsRemove方法,以便也可以使用项目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)

c# wpf

13
推荐指数
1
解决办法
1万
查看次数