开发者

WPF, How to properly unhook handlers when watching a TypeDescriptor

I am using WPF, and attempting to follow the MVVM pattern. Our team has decided to use the Xceed DataGrid control, and I am having some difficulties getting it to fit into the MVVM pattern.

One requirement that I have to meet is that I need to know when a user changes a column filter on the grid. I am aware that the latest version of the DataGrid control has an event that is raised for this, but unfortunatly, I have to use an older version of the control.

After searching for awhile, I found this post. It says that I need to hook a INotifyCollectionChanged handler to each of the possible list of filters. This works, but it also says that I need to unhook the handlers whenever the row source of the grid changes.

I was able to get it to work whe开发者_运维问答n I explicitly set the row source in the codebehind of the page (and in my first attempt in the ModelView using a direct reference to the view gasp!)

The first problem that I run into though, is how to do this without having the logic in the code behind or in the ViewModel. My solution was to extend the DataGridControl class and to add the following code:

    private IDictionary<string, IList> _GridFilters = null;
    public MyDataGridControl() : base()
    {
        TypeDescriptor.GetProperties(typeof(MyDataGridControl))["ItemsSource"].AddValueChanged(this, new EventHandler(ItemsSourceChanged));
    }

    void ItemsSourceChanged(object sender, EventArgs e)
    {
        UnsetGridFilterChangedEvent();
        SetGridFilterChangedEvent();
    }

    public void SetGridFilterChangedEvent()
    {
        if (this.ItemsSource == null)
            return;

        DataGridCollectionView dataGridCollectionView = (DataGridCollectionView)this.ItemsSource;

        _GridFilters = dataGridCollectionView.AutoFilterValues;

        foreach (IList autofilterValues in _GridFilters.Values)
        {
            ((INotifyCollectionChanged)autofilterValues).CollectionChanged += FilterChanged;
        }
    }

    /*TODO: Possible memory leak*/
    public void UnsetGridFilterChangedEvent()
    {
        if (_GridFilters == null)
            return;

        foreach (IList autofilterValues in _GridFilters.Values)
        {
            INotifyCollectionChanged notifyCollectionChanged = autofilterValues as INotifyCollectionChanged;

            notifyCollectionChanged.CollectionChanged -= FilterChanged;
        }

        _GridFilters = null;
    }

This lead me to my next problem; I'm pretty sure that by the time the ItemsSourceChanged method is called, the collection of AutoFilterValues has already changed, so I can not effectively unhook the handlers.

Am I right in assuming this? And can anyone think of a better way of managing these handlers while still allowing me to keep that functionality encapsulated within my extended class?

Sorry about the length of the post, and thanks in advance for the help!

-Funger


You are correct that AutoFilterValues will have already changed at that point, so you will be unhooking the wrong handlers, resulting in a memory leak.

The solution is very easy. Do exactly what you are doing but use an List<IList> instead of just referencing AutoFilterValues:

private List<IList> _GridFilters;

and use ToList() to make a copy of the filters you set handlers on:

_GridFilters = dataGridCollectionView.AutoFilterValues.Values.ToList();

Since _GridFilters is now a List<IList>, you'll also have to change the loops:

foreach(IList autofilterValues in _GridFilters) 
  ...

The reason this works is that the actual list of old filter lists is copied into _GridFilters, rather than simply referencing the AutoFilterValues property.

This is a nice general technique that is applicable in many situations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜