Exemplo n.º 1
0
        public static void OnIsAutoscrollChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var val  = (bool)e.NewValue;
            var lb   = s as ListBox;
            var data = lb.Items.SourceCollection as INotifyCollectionChanged;

            var autoscroller = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
            {
                if (lb.Items.Count > 0)
                {
                    lb.UpdateLayout();
                    lb.ScrollIntoView(lb.Items[lb.Items.Count - 1]);
                    lb.UpdateLayout();
                }
            });

            if (val)
            {
                data.CollectionChanged += autoscroller;
            }
            else
            {
                data.CollectionChanged -= autoscroller;
            }
        }
Exemplo n.º 2
0
        void RaiseNotifyCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs)
        {
            RaiseEvent(() =>
            {
                // NotifyCollectionChangedEventHandler
                System.Collections.Specialized.NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChangedEventHandler;
                if (collectionChanged != null)
                {
                    /*
                     * System.Diagnostics.Debug.WriteLine(
                     *  string.Format("OldStartingIndex:{0} NewStartingIndex:{1}",
                     *  eventArgs.OldStartingIndex,
                     *  eventArgs.NewStartingIndex));
                     * */

                    // it looks that .NET controls does not like 'batch' add/removes so we will have to
                    // split the notification in single items
                    if ((eventArgs.NewItems != null && eventArgs.NewItems.Count > 1) ||
                        (eventArgs.OldItems != null && eventArgs.OldItems.Count > 1))
                    {
                        System.Collections.IList items = eventArgs.Action == NotifyCollectionChangedAction.Remove ?
                                                         eventArgs.OldItems :
                                                         eventArgs.NewItems;
                        int startingIndex = eventArgs.Action == NotifyCollectionChangedAction.Remove ?
                                            eventArgs.OldStartingIndex :
                                            eventArgs.NewStartingIndex;

                        int nextIndex = 0;
                        foreach (object item in items)
                        {
                            List <object> singleItemList = new List <object>(new object[] { item });

                            System.Collections.Specialized.NotifyCollectionChangedEventArgs singleItemEventArg;
                            if (eventArgs.Action == NotifyCollectionChangedAction.Replace)
                            {
                                singleItemEventArg = new System.Collections.Specialized.NotifyCollectionChangedEventArgs(
                                    NotifyCollectionChangedAction.Replace,
                                    singleItemList,
                                    new List <object>(new object[] { eventArgs.OldItems[nextIndex] }),
                                    startingIndex + nextIndex);
                            }
                            else
                            {
                                singleItemEventArg = new System.Collections.Specialized.NotifyCollectionChangedEventArgs(
                                    eventArgs.Action,
                                    singleItemList,
                                    startingIndex + nextIndex);
                            }
                            // safe notify with just one item
                            collectionChanged(this, singleItemEventArg);
                            ++nextIndex;
                        }
                    }
                    else
                    {
                        collectionChanged(this, eventArgs);
                    }
                }
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listBox = s as ListBox;
            var listBoxItems = listBox.Items;
            var data = listBoxItems as INotifyCollectionChanged;

            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
                {
                    if (listBox.Items.Count > 0)
                    {
                        object lastItem = listBox.Items[listBox.Items.Count - 1];
                        listBox.ScrollIntoView(lastItem);
                    }
                });

            if ((bool)e.NewValue)
            {
                data.CollectionChanged += scrollToEndHandler;
            }
            else
            {
                data.CollectionChanged -= scrollToEndHandler;
            }
        }
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listBox = s as ListView;
            var listBoxItems = listBox.Items;
            var data = listBoxItems.SourceCollection as INotifyCollectionChanged;

            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
                {
                    if (listBox.Items.Count > 0)
                    {
                        try
                        {
                            object lastItem = listBoxItems[listBoxItems.Count - 1];

                            listBoxItems.MoveCurrentTo(lastItem);
                            listBox.ScrollIntoView(lastItem);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex);
                        }
                    }
                });

            if ((bool)e.NewValue)
                data.CollectionChanged += scrollToEndHandler;
            else
                data.CollectionChanged -= scrollToEndHandler;
        }
Exemplo n.º 5
0
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            // Be nice - use BlockReentrancy like MSDN said
            using (BlockReentrancy())
            {
                System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                if (eventHandler == null)
                {
                    return;
                }

                Delegate[] delegates = eventHandler.GetInvocationList();
                // Walk thru invocation list
                foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates)
                {
                    DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                    // If the subscriber is a DispatcherObject and different thread
                    if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                    {
                        // Invoke handler in the target dispatcher's thread
                        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
                    }
                    else // Execute handler as is
                    {
                        handler(this, e);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public static void OnIsAutoscrollChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var val = (bool)e.NewValue;
            var lb = s as ListBox;
            var ic = lb.Items;
            var data = ic.SourceCollection as INotifyCollectionChanged;

            var autoscroller = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) => {
                    object selectedItem = default(object);
                    switch (e1.Action) {
                        case NotifyCollectionChangedAction.Add:
                        case NotifyCollectionChangedAction.Move:
                            selectedItem = e1.NewItems[e1.NewItems.Count - 1];
                            break;
                        case NotifyCollectionChangedAction.Remove:
                            if (ic.Count < e1.OldStartingIndex) {
                                selectedItem = ic[e1.OldStartingIndex - 1];
                            }
                            else if (ic.Count > 0) selectedItem = ic[0];
                            break;
                        case NotifyCollectionChangedAction.Reset:
                            if (ic.Count > 0) selectedItem = ic[0];
                            break;
                    }

                    if (selectedItem != default(object)) {
                        ic.MoveCurrentTo(selectedItem);
                        lb.ScrollIntoView(selectedItem);
                    }
                });

            if (val) data.CollectionChanged += autoscroller;
            else data.CollectionChanged -= autoscroller;
        }
Exemplo n.º 7
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listBox      = s as ListBox;
            var listBoxItems = listBox.Items;
            var data         = listBoxItems.SourceCollection as INotifyCollectionChanged;

            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
            {
                if (listBox.Items.Count > 0)
                {
                    object lastItem = listBox.Items[listBox.Items.Count - 1];
                    listBoxItems.MoveCurrentTo(lastItem);
                    listBox.ScrollIntoView(lastItem);
                }
            });

            if ((bool)e.NewValue)
            {
                data.CollectionChanged += scrollToEndHandler;
            }
            else
            {
                data.CollectionChanged -= scrollToEndHandler;
            }
        }
 /// <summary>
 /// Fires the CollectionChanged event
 /// </summary>
 protected virtual void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs eventArgs)
 {
     System.Collections.Specialized.NotifyCollectionChangedEventHandler handler = this.CollectionChanged;
     if ((handler != null))
     {
         handler.Invoke(this, eventArgs);
     }
 }
        public static void OnIsAutoscrollChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var val  = (bool)e.NewValue;
            var lb   = s as ListBox;
            var ic   = lb.Items;
            var data = ic.SourceCollection as INotifyCollectionChanged;

            var autoscroller = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
            {
                object selectedItem = default(object);
                switch (e1.Action)
                {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Move:
                    selectedItem = e1.NewItems[e1.NewItems.Count - 1];
                    break;

                case NotifyCollectionChangedAction.Remove:
                    if (ic.Count < e1.OldStartingIndex)
                    {
                        selectedItem = ic[e1.OldStartingIndex - 1];
                    }
                    else if (ic.Count > 0)
                    {
                        selectedItem = ic[0];
                    }

                    break;

                case NotifyCollectionChangedAction.Reset:
                    if (ic.Count > 0)
                    {
                        selectedItem = ic[0];
                    }
                    break;
                }

                if (selectedItem != default(object))
                {
                    ic.MoveCurrentTo(selectedItem);
                    lb.ScrollIntoView(selectedItem);
                }
            });

            if (val)
            {
                data.CollectionChanged += autoscroller;
            }
            else
            {
                data.CollectionChanged -= autoscroller;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Remove o evento registrado para ser acionado quando a coleção for alterada.
 /// </summary>
 /// <param name="eventHandler"></param>
 void INotifyCollectionChangedDispatcher.RemoveCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler)
 {
     if (this.IsThreadSafe)
     {
         _collectionChangedHandlers.Remove(eventHandler);
     }
     else
     {
         _collectionChanged -= eventHandler;
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listView = s as Control;

#if Methord2 //Methord1
            var listBoxItems       = listView.Items;
            var data               = listBoxItems.SourceCollection as INotifyCollectionChanged;
            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
            {
                if (listView.Items.Count > 0)
                {
                    object lastItem = listView.Items[listView.Items.Count - 1];
                    listBoxItems.MoveCurrentTo(lastItem);
                    listView.ScrollIntoView(lastItem);
                }
            });

            if ((bool)e.NewValue)
            {
                data.CollectionChanged += scrollToEndHandler;
            }
            else
            {
                data.CollectionChanged -= scrollToEndHandler;
            }
#else
            var scrollChangedEventHander = new System.Windows.Controls.ScrollChangedEventHandler(
                (s2, e2) =>
            {
                if (e2.ExtentHeightChange > 0.0)
                {
                    ((ScrollViewer)e2.OriginalSource).ScrollToEnd();
                }
                e2.Handled = true;
            });
            if ((Boolean)e.NewValue)
            {
                listView.AddHandler(ScrollViewer.ScrollChangedEvent, scrollChangedEventHander);
            }
            else
            {
                listView.RemoveHandler(ScrollViewer.ScrollChangedEvent, scrollChangedEventHander);
            }
#endif
        }
Exemplo n.º 12
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var itemsControl = s as ItemsControl;
            var items        = itemsControl.Items;
            var data         = items.SourceCollection as INotifyCollectionChanged;

            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
            {
                if (itemsControl.Items.Count > 0)
                {
                    object lastItem = itemsControl.Items[itemsControl.Items.Count - 1];
                    items.MoveCurrentTo(lastItem);

                    if (s is ListBox)
                    {
                        ((ListBox)itemsControl).ScrollIntoView(lastItem);
                        return;
                    }

                    if (s is DataGrid)
                    {
                        ((DataGrid)itemsControl).ScrollIntoView(lastItem);
                        return;
                    }

                    var container = itemsControl.ItemContainerGenerator.ContainerFromItem(lastItem) as FrameworkElement;
                    if (container != null)
                    {
                        container.BringIntoView();
                    }
                }
            });

            if ((bool)e.NewValue)
            {
                data.CollectionChanged += scrollToEndHandler;
            }
            else
            {
                data.CollectionChanged -= scrollToEndHandler;
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            ListBox listBox = s as ListBox;
            ItemCollection listBoxItems = listBox.Items;
            INotifyCollectionChanged data = listBoxItems.SourceCollection as INotifyCollectionChanged;

            NotifyCollectionChangedEventHandler scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
                {
                    if (listBox.Items.Count > 0)
                    {
                        object lastItem = listBox.Items[listBox.Items.Count - 1];
                        listBoxItems.MoveCurrentTo(lastItem);
                        listBox.ScrollIntoView(lastItem);
                    }
                });

            if ((bool)e.NewValue)
                data.CollectionChanged += scrollToEndHandler;
            else
                data.CollectionChanged -= scrollToEndHandler;
        }
Exemplo n.º 14
0
 /// <summary>
 /// Adiciona o evento que será acionado quando a coleção for alterada.
 /// </summary>
 /// <param name="eventHandler"></param>
 /// <param name="priority"></param>
 void INotifyCollectionChangedDispatcher.AddCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler, NotifyCollectionChangedDispatcherPriority priority)
 {
     if (this.IsThreadSafe)
     {
         if (eventHandler != null)
         {
             var dispatcher = Threading.DispatcherManager.Dispatcher;
             if (dispatcher != null && !dispatcher.CheckAccess())
             {
                 dispatcher = null;
             }
             if (!_collectionChangedHandlers.ContainsKey(eventHandler))
             {
                 _collectionChangedHandlers.Add(eventHandler, new CollectionChangedWrapperEventData(dispatcher, eventHandler, priority));
             }
         }
     }
     else
     {
         _collectionChanged += eventHandler;
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// This method will be called when the AutoScrollToEnd
        /// property was changed
        /// </summary>
        /// <param name="s">The sender (the ListBox)</param>
        /// <param name="e">Some additional information</param>
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var itemsControl = s as ItemsControl;
            var items = itemsControl.Items;
            var data = items.SourceCollection as INotifyCollectionChanged;

            var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                (s1, e1) =>
                {
                    if (itemsControl.Items.Count > 0)
                    {
                        object lastItem = itemsControl.Items[itemsControl.Items.Count - 1];
                        items.MoveCurrentTo(lastItem);

                        if (s is ListBox)
                        {
                            ((ListBox)itemsControl).ScrollIntoView(lastItem);
                            return;
                        }

                        if (s is DataGrid)
                        {
                            ((DataGrid)itemsControl).ScrollIntoView(lastItem);
                            return;
                        }

                        var container = itemsControl.ItemContainerGenerator.ContainerFromItem(lastItem) as FrameworkElement;
                        if (container != null)
                        {
                            container.BringIntoView();
                        }
                    }
                });

            if ((bool)e.NewValue)
                data.CollectionChanged += scrollToEndHandler;
            else
                data.CollectionChanged -= scrollToEndHandler;
        }
Exemplo n.º 16
0
        // this collection also reacts to changes in its components' properties

        public ObservableCollectionEx() : base()
        {
            CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ObservableCollectionEx_CollectionChanged);
        }
Exemplo n.º 17
0
        async protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (this.DataContext == null)
            {
                Guid   conversationId;
                string senderName = null;

                base.OnNavigatedTo(e);
                conversationId = Guid.Parse(NavigationContext.QueryString["conversationId"]);

                if (NavigationContext.QueryString.ContainsKey("recipientName"))
                {
                    senderName = NavigationContext.QueryString["recipientName"];
                }

                Guid messageId = Guid.Empty;
                if (NavigationContext.QueryString.ContainsKey("messageId"))
                {
                    messageId = Guid.Parse(NavigationContext.QueryString["messageId"]);
                }

                int recipient = -1;
                if (NavigationContext.QueryString.ContainsKey("recipientId"))
                {
                    recipient = Int32.Parse(NavigationContext.QueryString["recipientId"]);
                }

                bool?isGroup = null;
                if (NavigationContext.QueryString.ContainsKey("isGroup"))
                {
                    isGroup = Boolean.Parse(NavigationContext.QueryString["isGroup"]);
                }

                int pivot = 0;
                if (NavigationContext.QueryString.ContainsKey("pivot"))
                {
                    pivot = Int32.Parse(NavigationContext.QueryString["pivot"]);
                }

                if (messageId != Guid.Empty)
                {
                    //DataSync.Instance.SyncMessageId(conversationId, messageId);
                }

                // Before the messages are loaded, ensure that the scroll handler is set correctly.
                var scrollToEndHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
                    (s1, e1) =>
                {
                    if (this.messagesListBox.ItemsSource.Count > 0)
                    {
                        this.messagesListBox.UpdateLayout();
                        object lastItem = this.messagesListBox.ItemsSource[this.messagesListBox.ItemsSource.Count - 1];
                        this.messagesListBox.ScrollTo(lastItem);
                    }
                });


                //INotifyCollectionChanged listItems = this.messagesListBox.ItemsSource as INotifyCollectionChanged;
                //listItems.CollectionChanged += scrollToEndHandler;

                UserModel recipientUser = DataSync.Instance.GetUser(recipient);
                if (recipientUser == null)
                {
                    UserType userType = isGroup.HasValue && isGroup.Value ? UserType.Group : UserType.User;
                    recipientUser = new Models.UserModel()
                    {
                        Id = recipient, Name = senderName, UserType = userType
                    };
                }

                // Create the viewmodel and load the messages
                ConversationMessagesViewModel cvm = new ConversationMessagesViewModel(conversationId, recipientUser, isGroup);
                this.DataContext = cvm;
                cvm.LoadMessagesForConversations();

                if (cvm.IsGroup && cvm.Recipient != null && !cvm.Recipient.IsGroupMember)
                {
                    MessageBox.Show(Strings.NotAGroupMember);
                }

                // Bind the list items to a collection view source
                // This helps in sorting the messages based on post time
                // without having to maintain the list in the view model in a sorted order
                CollectionViewSource messageCollection = new CollectionViewSource();
                messageCollection.Source = ((ConversationMessagesViewModel)this.DataContext).Messages;
                System.ComponentModel.SortDescription sort = new System.ComponentModel.SortDescription("PostDateTime", System.ComponentModel.ListSortDirection.Ascending);
                messageCollection.SortDescriptions.Add(sort);
                messagesListBox.ItemsSource = ((ConversationMessagesViewModel)this.DataContext).Messages;
                this.ConversationPagePivot.SelectedIndex = pivot;

                (Application.Current as App).watcher.PositionChanged += watcher_PositionChanged;
                (Application.Current as App).watcher.StatusChanged   += watcher_StatusChanged;
                (Application.Current as App).Currentlocation          = await(Application.Current as App).watcher.GetGeopositionAsync();
            }
        }