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

3 posts, 0 answers
  1. Rick
    Rick avatar
    2 posts
    Member since:
    Oct 2010

    Posted 09 Apr 2012 Link to this post

    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. Tarik
    Tarik avatar
    12 posts
    Member since:
    May 2015

    Posted 27 Aug 2015 Link to this post

    Hi,

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

    regards.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 27 Aug 2015 Link to this post

    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
Back to Top