基于C# wpf 實(shí)現(xiàn)Grid內(nèi)控件拖動(dòng)詳情
前言:
有一些業(yè)務(wù)場(chǎng)景中我們需要拖動(dòng)控件,在Grid中就可以實(shí)現(xiàn)控件拖動(dòng),通過(guò)設(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.注冊(cè)鼠標(biāo)事件
拖動(dòng)的控件需要注冊(cè)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é):
本文說(shuō)明了Grid中控件拖動(dòng)的方法。方法采用了記錄鼠標(biāo)位置以及控件位置的方法來(lái)確保準(zhǔn)確的相對(duì)位置。如果是采用只記錄鼠標(biāo)位置,計(jì)算時(shí)通過(guò)控件內(nèi)部坐標(biāo)差值累加,這樣會(huì)產(chǎn)生累計(jì)誤差,除非取整運(yùn)算,但取整與dpi有可能產(chǎn)生不兼容??偟膩?lái)說(shuō),本方法采用準(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)文章
C#控件編程之顯示信息控件詳解(Label、LinkLabel)
這篇文章主要介紹了C#控件編程之顯示信息控件詳解(Label、LinkLabel),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Unity中的RegisterPlugins實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity中的RegisterPlugins實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
基于Unity實(shí)現(xiàn)2D邊緣檢測(cè)
這篇文章主要介紹了如何利用Unity實(shí)現(xiàn)2D邊緣檢測(cè),從而達(dá)到人物描邊效果。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04
使用C#實(shí)現(xiàn)上位機(jī)與PLC通信的過(guò)程詳解
隨著工業(yè)自動(dòng)化技術(shù)的不斷發(fā)展,PLC(可編程邏輯控制器)已成為現(xiàn)代生產(chǎn)過(guò)程中不可或缺的設(shè)備,為了實(shí)現(xiàn)設(shè)備之間的數(shù)據(jù)交換和遠(yuǎn)程控制,上位機(jī)系統(tǒng)需要與PLC進(jìn)行通信,在本文中,我們將從零開始,介紹如何使用C#實(shí)現(xiàn)上位機(jī)與PLC的通信,需要的朋友可以參考下2025-01-01

