基于C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)詳情
前言:
有一些業(yè)務(wù)場景中我們需要拖動(dòng)控件,在Grid
中就可以實(shí)現(xiàn)控件拖動(dòng),通過設(shè)置Margin
屬性即可,根據(jù)鼠標(biāo)的移動(dòng),設(shè)置相應(yīng)的Margin
的Left
、Top,當(dāng)然有時(shí)也不是直接設(shè)置的,需要根據(jù)HorizontalAlignment
、VerticalAlignment
值有不同的計(jì)算方法。
一、如何實(shí)現(xiàn)?
1.注冊鼠標(biāo)事件
拖動(dòng)的控件需要注冊3個(gè)鼠標(biāo)事件分別是,鼠標(biāo)按下、鼠標(biāo)移動(dòng)、鼠標(biāo)彈起。
以Button為例:
<Button PreviewMouseDown="Button_MouseDown" PreviewMouseMove="Button_MouseMove" PreviewMouseUp="Button_MouseUp"> </Button>
2.記錄位置
在鼠標(biāo)按下事件中記錄位置。
//鼠標(biāo)是否按下 bool _isMouseDown = false; //鼠標(biāo)按下的位置 Point _mouseDownPosition; //鼠標(biāo)按下控件的Margin Thickness _mouseDownMargin; //鼠標(biāo)按下事件 private void Button_MouseDown(object sender, MouseButtonEventArgs e) { var c = sender as Control; _isMouseDown = true; _mouseDownPosition = e.GetPosition(this); _mouseDownMargin = c.Margin; }
3.跟隨鼠標(biāo)移動(dòng)
鼠標(biāo)按下后移動(dòng)鼠標(biāo),控件需要跟隨鼠標(biāo)移動(dòng)。根據(jù)HorizontalAlignment
、VerticalAlignment
值不同,計(jì)算Margin的方式也不同。
private void Button_MouseMove(object sender, MouseEventArgs e) { if (_isMouseDown) { var c = sender as Control; var pos = e.GetPosition(this); var dp = pos - _mouseDownPosition; double left, top, right, bottom; if (c.HorizontalAlignment == HorizontalAlignment.Stretch|| c.HorizontalAlignment == HorizontalAlignment.Center) //中央移動(dòng)距離是雙倍 { left= _mouseDownMargin.Left+ dp.X * 2; right = _mouseDownMargin.Right; } else if(c.HorizontalAlignment== HorizontalAlignment.Left) //左邊是正常距離 { left = _mouseDownMargin.Left + dp.X ; right = _mouseDownMargin.Right; } else //右邊是右邊距距離 { left = _mouseDownMargin.Left; right = _mouseDownMargin.Right - dp.X; } if (c.VerticalAlignment == VerticalAlignment.Stretch || c.VerticalAlignment == VerticalAlignment.Center) //中央移動(dòng)距離是雙倍 { top = _mouseDownMargin.Top+ dp.Y* 2; bottom = _mouseDownMargin.Bottom; } else if (c.VerticalAlignment == VerticalAlignment.Top) //頂部是正常距離 { top = _mouseDownMargin.Top + dp.Y ; bottom = _mouseDownMargin.Bottom; } else //底部是底邊距距離 { top = _mouseDownMargin.Top ; bottom = _mouseDownMargin.Bottom- dp.Y; } c.Margin = new Thickness(left, top, right, bottom); } }
4.恢復(fù)標(biāo)識(shí)
鼠標(biāo)彈起后需要恢復(fù)標(biāo)識(shí),讓控件不再跟隨鼠標(biāo)移動(dòng)。
private void Button_MouseUp(object sender, MouseButtonEventArgs e) { if (_isMouseDown) { _isMouseDown = false; //移動(dòng)了的控件不響應(yīng)點(diǎn)擊事件(此處根據(jù)具體需求) e.Handled = true; } }
二、示例
示例代碼:
<Window x:Class="WpfControlMove.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfControlMove" mc:Ignorable="d" Title="MainWindow" Height="360" Width="640"> <Grid> <Button Width="120" Height="50" Content="移動(dòng)" PreviewMouseDown="Button_MouseDown" PreviewMouseMove="Button_MouseMove" PreviewMouseUp="Button_MouseUp"> </Button> </Grid> </Window>
效果預(yù)覽:
總結(jié):
本文說明了Grid中控件拖動(dòng)的方法。方法采用了記錄鼠標(biāo)位置以及控件位置的方法來確保準(zhǔn)確的相對(duì)位置。如果是采用只記錄鼠標(biāo)位置,計(jì)算時(shí)通過控件內(nèi)部坐標(biāo)差值累加,這樣會(huì)產(chǎn)生累計(jì)誤差,除非取整運(yùn)算,但取整與dpi有可能產(chǎn)生不兼容??偟膩碚f,本方法采用準(zhǔn)確的位置計(jì)算方式,而且還根據(jù)不同停靠采用相應(yīng)的計(jì)算方法,適用性較好。
到此這篇關(guān)于基于C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)詳情的文章就介紹到這了,更多相關(guān)C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能
這篇文章主要為大家詳細(xì)介紹了Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04WinForm實(shí)現(xiàn)的圖片拖拽與縮放功能示例
這篇文章主要介紹了WinForm實(shí)現(xiàn)的圖片拖拽與縮放功能,結(jié)合具體實(shí)例形式分析了WinForm鼠標(biāo)事件響應(yīng)及圖片元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼示例
這篇文章主要介紹了C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼,結(jié)合實(shí)例形式分析了基于C#實(shí)現(xiàn)的base64編碼解碼操作相關(guān)技巧,需要的朋友可以參考下2017-01-01C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例
下面小編就為大家分享一篇C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12C#?winform跨線程操作控件的實(shí)現(xiàn)
本文主要介紹了C#?winform跨線程操作控件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06