Exemple #1
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            SelectionHelper.SetEnableSelection(this, true);

            UpdateCollumnHeader();


            #region Update scroll position if scroll bar disappear
            ScrollViewer scrollView = UITools.FindVisualChild <ScrollViewer>(this);

            DependencyPropertyDescriptor vertScrollbarVisibilityDescriptor =
                DependencyPropertyDescriptor.FromProperty(
                    ScrollViewer.ComputedVerticalScrollBarVisibilityProperty, typeof(ScrollViewer));

            vertScrollbarVisibilityDescriptor.AddValueChanged
                (scrollView, delegate
            {
                if (scrollView.ComputedHorizontalScrollBarVisibility == System.Windows.Visibility.Collapsed)
                {
                    VirtualizingPanel panel = UITools.FindVisualChild <VirtualizingPanel>(this);
                    if (panel is IScrollInfo)
                    {
                        (panel as IScrollInfo).SetVerticalOffset(0);
                    }
                }
            });

            DependencyPropertyDescriptor horzScrollbarVisibilityDescriptor =
                DependencyPropertyDescriptor.FromProperty(
                    ScrollViewer.ComputedHorizontalScrollBarVisibilityProperty, typeof(ScrollViewer));

            horzScrollbarVisibilityDescriptor.AddValueChanged
                (scrollView, delegate
            {
                if (scrollView.ComputedHorizontalScrollBarVisibility == System.Windows.Visibility.Collapsed)
                {
                    VirtualizingPanel panel = UITools.FindVisualChild <VirtualizingPanel>(this);
                    if (panel is IScrollInfo)
                    {
                        (panel as IScrollInfo).SetHorizontalOffset(0);
                    }
                }
            });


            #endregion

            #region ContextMenu

            Point mouseDownPt = new Point(-100, -100);

            this.AddHandler(TreeViewItem.PreviewMouseRightButtonDownEvent, new MouseButtonEventHandler(
                                (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args)
            {
                mouseDownPt = args.GetPosition(this);
            }));
            this.AddHandler(TreeViewItem.MouseRightButtonUpEvent, new MouseButtonEventHandler(
                                (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args)
            {
                Point mouseUpPt = args.GetPosition(this);

                if ((Math.Abs(mouseDownPt.X - mouseUpPt.X) < 5) &&
                    (Math.Abs(mouseDownPt.Y - mouseUpPt.Y) < 5))
                {
                    args.Handled = true;
                    if (ContextMenuCommand != null && ContextMenuCommand.CanExecute(this.DataContext))
                    {
                        if (SelectedValue != null)
                        {
                            ContextMenuCommand.Execute(this.DataContext);
                        }
                    }
                }
            }));

            #endregion

            //Memory Leak
            //Unloaded += (o, e) =>
            //{
            //    SelectionHelper.SetEnableSelection(o as FileList2, false);
            //    (o as FileList2).View = null;

            //};
        }
Exemple #2
0
        static void UpdateSelection(ScrollContentPresenter p, Rect selectionBounds, bool highlightOnly)
        {
            ListView lvControl = UITools.FindAncestor <ListView>(p);

            if (lvControl != null)
            {
                IChildInfo icInfo = UITools.FindVisualChild <Panel>(p) as IChildInfo;

                List <object> newList    = new List <object>();
                List <int>    newIntList = new List <int>();

                if (icInfo != null)
                {
                    for (int i = 0; i < lvControl.Items.Count; i++)
                    {
                        if (icInfo.GetChildRect(i).IntersectsWith(selectionBounds))
                        {
                            newList.Add(lvControl.Items[i]);
                            newIntList.Add(i);
                        }
                    }
                }
                else
                //0.4 For GridView, only support selection if drag occur inside the first column
                if (lvControl.View is GridView)
                {
                    Point curPos = (Point)PositionCheck(GetSelectionAdorner(p), Mouse.GetPosition(p));

                    if ((lvControl.View as GridView).Columns.Count > 0)
                    {
                        double col0width = (lvControl.View as GridView).Columns[0].ActualWidth;
                        if (curPos.X < col0width || GetStartPosition(p).X < col0width)
                        {
                            if (_firstSelectedItem == null)
                            {
                                _firstSelectedItem = getSelectedItem(p, curPos);
                            }
                            ListViewItem curSelectedItem = getSelectedItem(p, curPos);

                            if (_firstSelectedItem != null && curSelectedItem != null)
                            {
                                int startIdx = lvControl.ItemContainerGenerator.IndexFromContainer(_firstSelectedItem);
                                int endIdx   = lvControl.ItemContainerGenerator.IndexFromContainer(curSelectedItem);


                                for (int i = Math.Min(startIdx, endIdx); i <= Math.Max(startIdx, endIdx); i++)
                                {
                                    if (i >= 0)
                                    {
                                        newList.Add(lvControl.Items[i]);
                                        newIntList.Add(i);
                                    }
                                }
                            }
                        }
                    }
                }

                if (highlightOnly)
                {
                    SetHighlightCount(lvControl, newIntList.Count);
                    HighlightItems(lvControl, newIntList);
                }
                else
                {
                    SetHighlightCount(lvControl, 0);
                    UnhighlightItems(lvControl);
                    UpdateSelectedItems(lvControl, newList);
                }
            }
        }
Exemple #3
0
        static void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            ScrollContentPresenter p = getScrollContentPresenter(sender);

            if (e.ChangedButton == MouseButton.Left)
            {
                if (GetIsDragging(p))
                {
                    EndDragging(p);
                    Mouse.Capture(null);
                    UITools.FindAncestor <ListView>(p).Focus();

                    ListViewItem curSelectedItem = getSelectedItem(p, Mouse.GetPosition(p));
                    if (curSelectedItem != null && curSelectedItem.Equals(_itemUnderMouse))
                    {
                        ListView lvControl = UITools.FindAncestor <ListView>(p);
                        ClearSelection(lvControl);
                        _itemUnderMouse.IsSelected = true;
                    }
                }

                if (Point.Subtract(GetStartPosition(p), Mouse.GetPosition(p)).Length < 10.0d)
                {
                    ListView lvControl = UITools.FindAncestor <ListView>(p);
                    ClearSelection(lvControl);
                }

                if (_firstSelectedItem != null)
                {
                    _firstSelectedItem.IsSelected = true;
                }

                _firstSelectedItem = null;
                SetIsDragging(p, false);
            }
            else if (e.ChangedButton == MouseButton.Right)
            {
                ListView lvControl = UITools.FindAncestor <ListView>(p);

                ClearSelection(lvControl);

                if (_itemUnderMouse != null)
                {
                    _itemUnderMouse.IsSelected = false;
                }
                if (_firstSelectedItem != null)
                {
                    _firstSelectedItem.IsSelected = false;
                }

                _firstSelectedItem = null;
                SetIsDragging(p, false);
                return;
            }
            else
            {
            }



            #endregion

            #region Data


            #endregion
        }
Exemple #4
0
        public static void OnEnableSelectionpChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            if (s is ListView)
            {
                ListView control = s as ListView;


                var OnScrollChange = (RoutedEventHandler) delegate(object sender, RoutedEventArgs args)
                {
                    ItemsPresenter         ip = UITools.FindVisualChild <ItemsPresenter>(sender as ListView);
                    ScrollContentPresenter p  = UITools.FindAncestor <ScrollContentPresenter>(ip);

                    if (GetIsDragging(p) && Mouse.LeftButton == MouseButtonState.Pressed)
                    {
                        UpdatePosition(p, true);
                    }
                };

                var OnMouseDown = (RoutedEventHandler) delegate(object sender, RoutedEventArgs args)
                {
                    ClearSelection(sender as ListView);
                };

                var OnSizeChanged = (RoutedEventHandler) delegate(object sender, RoutedEventArgs args)
                {
                    ItemsPresenter         ip = UITools.FindVisualChild <ItemsPresenter>(sender as ListView);
                    ScrollContentPresenter p  = UITools.FindAncestor <ScrollContentPresenter>(ip);
                    EndDragging(p);
                };

                Action unloadAdorner = () =>
                {
                    ScrollContentPresenter p = GetLastScrollContentPresenter(control);
                    if (p != null)
                    {
                        SelectionAdorner _adorner = GetSelectionAdorner(p);
                        if (AdornerLayer.GetAdornerLayer(p) != null)
                        {
                            AdornerLayer.GetAdornerLayer(p).Remove(_adorner);
                        }

                        control.MouseUp           -= new MouseButtonEventHandler(OnMouseUp);
                        control.MouseMove         -= new MouseEventHandler(OnMouseMove);
                        _adorner.PreviewMouseDown -= new MouseButtonEventHandler(OnPreviewMouseDown);
                        _adorner.MouseMove        -= new MouseEventHandler(OnMouseMove);
                        _adorner.MouseUp          -= new MouseButtonEventHandler(OnMouseUp);
                        control.RemoveHandler(ListView.SizeChangedEvent, OnSizeChanged);

                        SetSelectionAdorner(p, null);
                    }
                };

                Action attachAdorner = () =>
                {
                    unloadAdorner();
                    ItemsPresenter         ip = UITools.FindVisualChild <ItemsPresenter>(control);
                    ScrollContentPresenter p  = UITools.FindAncestor <ScrollContentPresenter>(ip);
                    if (p != null)
                    {
                        SelectionAdorner _adorner = new SelectionAdorner(p);
                        SetSelectionAdorner(p, _adorner);

                        AdornerLayer.GetAdornerLayer(p).Add(_adorner);
                        control.PreviewMouseDown  += new MouseButtonEventHandler(OnPreviewMouseDown);
                        control.MouseUp           += new MouseButtonEventHandler(OnMouseUp);
                        control.MouseMove         += new MouseEventHandler(OnMouseMove);
                        _adorner.PreviewMouseDown += new MouseButtonEventHandler(OnPreviewMouseDown);
                        _adorner.MouseMove        += new MouseEventHandler(OnMouseMove);
                        _adorner.MouseUp          += new MouseButtonEventHandler(OnMouseUp);
                        control.AddHandler(ListView.SizeChangedEvent, OnSizeChanged);

                        SetLastScrollContentPresenter(control, p);
                    }
                };

                if ((bool)e.NewValue == true)
                {
                    if (control.IsLoaded)
                    {
                        attachAdorner();
                    }
                    else
                    {
                        control.Loaded += delegate { attachAdorner(); }
                    };

                    control.AddHandler(ScrollViewer.ScrollChangedEvent, OnScrollChange);
                    control.AddHandler(ListView.MouseDownEvent, OnMouseDown);


                    //Monitor view change, and reattach handlers.
                    DependencyPropertyDescriptor viewDescriptor = DependencyPropertyDescriptor.FromProperty(ListView.ViewProperty, typeof(ListView));

                    viewDescriptor.AddValueChanged
                        (control, delegate
                    {
                        control.Dispatcher.BeginInvoke(DispatcherPriority.Input, attachAdorner);
                    });
                }
                else //If EnableSelection = False
                {
                    unloadAdorner();
                    control.RemoveHandler(ScrollViewer.ScrollChangedEvent, OnScrollChange);
                    control.RemoveHandler(ListView.MouseDownEvent, OnMouseDown);

                    SetSelectionAdorner(control, null);
                }
            }
        }