This is a migrated thread and some comments may be shown as answers.

2 Issues - Cannot get DispatcherObject from WeakEvent.WeakListener and Cannot Insert Row when list is empty

2 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rick
Top achievements
Rank 1
Rick asked on 09 Apr 2012, 07:20 PM
I am currently having 2 issues with the RadGridView

1)
I am receiving calls from a WCF Service in the client that do the usual CRUD actions.  Of course this comes in on a different thread than the UI thread.  So I have a dispatchable observable collection with the code below.

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Windows.Threading;
 
    public class DispatchableObservableCollection<T>
        : ObservableCollection<T>
    {
        public DispatchableObservableCollection()
        {
        }
 
        public DispatchableObservableCollection(IEnumerable<T> collection)
            : base(collection)
        {
        }
 
        // Override the event so this class can access it
        public override event NotifyCollectionChangedEventHandler CollectionChanged;
 
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // Be nice - use BlockReentrancy like MSDN said
            using (BlockReentrancy())
            {
                NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                if (eventHandler != null)
                {
                    Delegate[] delegates = eventHandler.GetInvocationList();
                    // Walk thru invocation list
                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                        // If the subscriber is a DispatcherObject and different thread
                        if (dispatcherObject != null
                            && !dispatcherObject.CheckAccess())
                        {
                            // Invoke handler in the target dispatcher's thread
                            dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind,
                                                               handler,
                                                               this,
                                                               e);
                        }
                        else // Execute handler as is
                        {
                            handler(this,
                                    e);
                        }
                    }
                }
            }
        }
    }

The problem is the object subscribed to NotifyCollectionChanged event for the grid  is WeakEvent.WeakListener and the cast to DispatcherObject fails (null).   How do I get the DispatcherObject from handler.Target.  I'm trying to make this work so that I don't have to use Application.Current.Dispatcher as a bail out so that it will work for UI objects not on the primary UI thread.  Is there a work around?


2)

I have detected some odd behavior for add new item.  If I have a grid and remove all items and then want to add more back into the grid it works fine.  However, if I remove all items from the collection and then close the application and restart when I click the add new item header nothing happens.  i.e.,  a new row is not added.  The collection I am using is the one above and the collection is there (Not null), it is just empty.  Thoughts?


Thanks for the help,

Rick
 

2 Answers, 1 is accepted

Sort by
0
Tarik
Top achievements
Rank 1
answered on 27 Aug 2015, 01:51 PM

Hi,

I have the same first issue as explained by Rick, plz is there any solution for it ?

regards.

0
Ivan Ivanov
Telerik team
answered on 27 Aug 2015, 02:18 PM
Hello Tarik,

Do you have a valid subscriber for the event that is a DispatcherObject? I prepared a dummy sample project that illustrates how to implement it.

Regards,
Ivan Ivanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Rick
Top achievements
Rank 1
Answers by
Tarik
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or