如何观察对基础数组的更改

如何观察对基础数组的更改

本文介绍了如何观察对基础数组的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个包含各种类的旧式库,其中一个类的属性类型为int[].此数组属性最多可以包含数百万个元素.我希望使用WPF和C#可视化此数组.问题是,如果我将该数组绑定到ItemsControl,则不会收到任何更改通知.另一方面,如果包装到ObservableCollection中,那么我将无法观察到基础数组的更改(这些更改的发生是由于库本身,而不是因为我的代码).

有人可以提出解决方案吗?

干杯,
George.

Hello everybody,

I have a legacy library containing various classes, one of which has a property of type int[]. This array property may contain up to millions of elements. I wish to visualize this array using WPF and C#. The problem is that if I bind that array to an ItemsControl then I don''t get any change notifications. On the other hand, if I wrap into an ObservableCollection then I cannot observe changes to the underlying array (these changes happen because of the library itself, not because of my code).

Can anybody suggest a solution?

Cheers,
George.

推荐答案


public class ArrayItem : IObservable
{
    private Array[int] m_array = null;
    private int m_value;
 
    public int Value
    {
        get
        {
            return m_value;
        }
        set
        {
            m_value = value;
            OnPropertyChanged("Value");
        }
    }
 
    public int Index { get; set; }
 
    //--------------------------------------------------------------------
    public void OnPropertyChanged(string propertyName)
    {
        // blah blah
    }
 
    //--------------------------------------------------------------------
    public ArrayItem(Array[] int source, int index)
    {
        m_array = source;
        Index   = index;
        Value   = m_array[Index];
    }

 
    //--------------------------------------------------------------------
    // call this method from the thread when the array value changes
    public UpdateValue()
    {
        Value   = m_array[Index];
    }
}



当然,如果数组是静态的,则可以避免传入数组,并且可以在应用程序中的任何地方访问它.



Of course, you could probably avoid passing in the array if it''s static and you can get to it anywhere in your app.


这篇关于如何观察对基础数组的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 17:41