首页 \ 问答 \ 可观察的LinkedList(Observable LinkedList)

可观察的LinkedList(Observable LinkedList)

在我的WPF应用程序中,我有一个ItemsControl,其项目值取决于显示的上一个项目。

ViewModel是一个音频文件,分成可变长度的部分,我需要以这种方式显示它,右边显示一个DateTime,这就是我需要计算的东西(我只知道每个部分的长度,我需要计算它开始和结束的实际时间,以及ItemsControl上的位置)。

--
  ----
      ------------
                  --
                    --------------------

我的第一种方法是使用ObservableCollection<MyviewModel>但很快就发生了一些恐怖事件:

5路多重绑定,其中的IMultiValueConverter我计算要返回的值并将DataContext的属性设置为该值,因为我只知道运行时的前一个元素。

前一个元素是使用Relativesource.PreviousData上的绑定发送的。

现在我的问题是,从转换器设置一个值(这显然是一件坏事),并实际上让它工作,一个常规的集合没有在其元素中的顺序概念,所以当进一步的道路时,我想在其余部分中间添加一个音频部分,显示器搞砸了。

此外,当我实现更多业务逻辑时,我可能需要访问在此转换器中计算的音频部分的开始和结束,以及如果它还没有显示怎么办?

所以这种方法在几个层面上都是错误的。

这就是我开始谷歌搜索并发现LinkedList 。 现在我正在尝试创建一个基本上是Observable LinkedList的类(我不需要它是通用的):

public class ObservableSegmentLinkedList : LinkedList<MyViewModel>, INotifyCollectionChanged
    {
        //Overrides ???

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public void OnNotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }

        #endregion
    }

问题的核心是我无法覆盖修改集合的方法(Addfirst,AddLast等),所以我无法正确调用OnNotifyCollectionChanged ...

所以我想我可以为这些方法做出重载,但这听起来很讨厌......

简而言之:我需要某种集合,其中每个项目都知道前一个项目的细节,以便计算其自己的属性之一。

有什么线索吗? 这是一个很好的解决方案吗?

谢谢!

附录,ViewModel看起来像:

public class MyViewModel : INotifyPropertyChanged
    {
        private DateTime m_SegmentLength;
        public DateTime SegmentLength
        {
            get { return m_SegmentLength; }
            set
            {
                m_SegmentLength = value;
                NotifyPropertyChanged("SegmentLength");
            }
        }

        private DateTime m_SegmentAdvert;
        public DateTime SegmentAdvert
        {
            get { return m_SegmentAdvert; }
            set
            {
                m_SegmentAdvert = value;
                NotifyPropertyChanged("SegmentAdvert");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String prop)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        #endregion
    }

编辑:我想我会尝试结合托马斯和威尔的答案:我将使用组合(即我在自定义对象中保留LinkedList的实例而不是继承它)并重新定义要使用的方法(AddAfter,AddFirst等)在调用实际的LinkedList方法之后我将调用OnNotifyPropertychanged。 这有点工作,但我想我的问题不会有任何优雅的解决方案......


In my WPF app, I have an ItemsControl whose items values are dependant upon the previous item displayed.

The ViewModel is an audio file split into parts of variable length, and i need to display it in such manner, with a DateTime displayed on the right, and that's what i need to calculate (I only know each part's length, i need to calculate the actual time it starts and ends, and the position on the ItemsControl).

--
  ----
      ------------
                  --
                    --------------------

My first approach was to use an ObservableCollection<MyviewModel> but soon enough some horrors occured :

5-way multibinding in which's IMultiValueConverter I'd calculate the value to return and set a property of the DataContext to that value, because I only knew the previous element at runtime.

The previous element was sent using a binding on Relativesource.PreviousData.

Now my problem is that after setting a value from the Converter (which is obviously a bad thing), and actually getting it to work, a regular Collection doesn't have a notion of order in its elements, so when further down the road when i want to add an audio part in the middle of the rest, the display is messed up.

Furthermore, when I'll implement more business logic, I may need to access the audio parts's start and end that are calculated in this converter, and what if it's not displayed yet...?

So that approach was wrong on several levels.

That's where i started googling and found out about LinkedList. Now I'm trying to make a class that is basically an Observable LinkedList (I don't need it to be generic):

public class ObservableSegmentLinkedList : LinkedList<MyViewModel>, INotifyCollectionChanged
    {
        //Overrides ???

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public void OnNotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }

        #endregion
    }

And the heart of the problem is that i can't override the methods that modify the collection (Addfirst, AddLast etc), so i can't call OnNotifyCollectionChanged properly...

So I'm thinking i could make overloads for each of these methods, but that sounds quite nasty...

In short: I need some kind of collection in which each item knows details of the previous one in order to calculate one of its own properties.

Any clues? is this even a good solution?

Thanks!

Appendix, the ViewModel looks like:

public class MyViewModel : INotifyPropertyChanged
    {
        private DateTime m_SegmentLength;
        public DateTime SegmentLength
        {
            get { return m_SegmentLength; }
            set
            {
                m_SegmentLength = value;
                NotifyPropertyChanged("SegmentLength");
            }
        }

        private DateTime m_SegmentAdvert;
        public DateTime SegmentAdvert
        {
            get { return m_SegmentAdvert; }
            set
            {
                m_SegmentAdvert = value;
                NotifyPropertyChanged("SegmentAdvert");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String prop)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        #endregion
    }

EDIT: i think i will try to combine Thomas and Will's answers: I'll use composition (i.e I keep an instance of LinkedList in my custom object instead of inheriting from it) and redefine methods that are meant to be used (AddAfter, AddFirst etc) in which i'll just call OnNotifyPropertychanged after calling the actual LinkedList method. It's a bit of work but i guess there won't be any elegant solution to my problem...


原文:https://stackoverflow.com/questions/6996425
更新时间:2023-08-20 16:08

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。