第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何單獨拖動 SelectedItem 附近的列表框項目

如何單獨拖動 SelectedItem 附近的列表框項目

C#
三國紛爭 2021-10-24 19:36:04
我想拖動列表框選定的項目。拖動功能工作正常。我的要求是從列表框中的任何其他位置開始拖動時不應發(fā)生拖動。我已經(jīng)這樣做了,但這不起作用。請任何人建議我實現(xiàn)這一目標,        private bool IsDragging { get; set; }        private Point _startPoint { get; set; }                protected override void OnAttached()        {                                        this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;            this.AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;        }        private void AssociatedObject_PreviewMouseMove(object sender, MouseEventArgs e)        {            if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)            {                Point position = e.GetPosition(null);                                      if (Math.Abs(position.X - _startPoint.X) <= SystemParameters.MinimumHorizontalDragDistance &&                    Math.Abs(position.Y - _startPoint.Y) <= SystemParameters.MinimumVerticalDragDistance)                {                    StartDrag(sender);                }            }        }        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            _startPoint = e.GetPosition(null);        }        private void StartDrag(object sender)        {            IsDragging = true;            if (sender is ListBox)            {                var listBox = (sender as ListBox);                if (listBox != null)                {                    var selectedMember = listBox.SelectedItem;                    if (selectedMember != null)                    {                        DragDrop.DoDragDrop(listBox, selectedMember, DragDropEffects.Copy);                    }                }            }            IsDragging = false;        }
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經(jīng)驗 獲得超13個贊

這樣做,通過使用System.Windows.Media.VisualTreeHelper.HitTest(this, point)它給出鼠標下的當前元素。我想它會幫助你。


private bool IsDrag { get; set; }              


        protected override void OnAttached()

        {            

            this.AssociatedObject.Drop += AssociatedObject_Drop;            

            this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;

            this.AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;


        }


        private void AssociatedObject_PreviewMouseMove(object sender, MouseEventArgs e)

        {                                  

            if (e.LeftButton == MouseButtonState.Pressed)

            {               

                if(IsDrag)

                {

                    StartDrag(sender);

                }

            }

        }


        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            IsDrag = false;    


            if(sender is ListBox)

            {

                Point initialPoint = e.GetPosition((UIElement)sender);

                var histPoint = VisualTreeHelper.HitTest(sender as ListBox, initialPoint);


                if (histPoint.VisualHit != null)

                {

                    if (histPoint.VisualHit is TextBlock || histPoint.VisualHit is Border)

                    {

                        IsDrag = true;

                    }

                }

            }            

        }                   


        private void StartDrag(object sender)

        {            

            if (sender is ListBox)

            {

                var listBox = (sender as ListBox);

                if (listBox != null)

                {

                    var selectedMember = listBox.SelectedItem;

                    if (selectedMember != null)

                    {

                        DragDrop.DoDragDrop(listBox, selectedMember, DragDropEffects.Copy);

                    }

                }

            }           

        }



查看完整回答
反對 回復 2021-10-24
  • 1 回答
  • 0 關(guān)注
  • 194 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號