예제 #1
0
        public DirectoryTree2()
        {
            #region ContextMenu
            this.AddHandler(TreeViewItem.MouseRightButtonUpEvent, new MouseButtonEventHandler(
                                (MouseButtonEventHandler) delegate(object sender, MouseButtonEventArgs args)
            {
                TreeViewItem sourceItem = UITools.GetParentTreeViewItem(args.OriginalSource as FrameworkElement);
                if (sourceItem != null)
                {
                    if (!sourceItem.IsSelected)
                    {
                        TreeViewItemAutomationPeer peer   = new TreeViewItemAutomationPeer(sourceItem);
                        ISelectionItemProvider invokeProv = peer.GetPattern(PatternInterface.SelectionItem) as ISelectionItemProvider;
                        invokeProv.Select();
                    }

                    if (sourceItem.IsSelected)
                    {
                        if (ContextMenuCommand != null && ContextMenuCommand.CanExecute(this.SelectedItem))
                        {
                            ContextMenuCommand.Execute(this.SelectedItem);
                        }
                    }
                }
            }));

            #endregion

            W7TreeViewItemUtils.SetIsEnabled(this, true);
        }
예제 #2
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;

            //};
        }