当前位置: 首页 > news >正文

WPF实现组件拖动(Grid)

xaml:

<Window x:Class="WpfApp3Test.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"
mc:Ignorable="d"
Title="MainWindow"
Height="600" Width="800"
WindowStartupLocation="CenterScreen">


<Window.Resources>
<Style x:Key="BorderStyle" TargetType="Border">
<Setter Property="Height" Value="30"/>
<Setter Property="Background" Value="YellowGreen"/>
</Style>

<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Yellow"/>
<Setter Property="FontSize" Value="20"/>
</Style>
</Window.Resources>

<Grid x:Name="parentGrid">

<!-- 组件A -->
<Canvas x:Name="ComponentA" MouseLeftButtonDown="A_MouseLeftButtonDown">
<Rectangle Width="500" Height="300" Fill="LightBlue" />
<TextBlock Canvas.Left="20" Canvas.Top="20" Text="组件A(Canvas)" FontSize="16" />
</Canvas>

<!-- 组件B 使用 Margin 进行定位 -->
<Border x:Name="ComponentB"
Width="300" Height="250"
BorderBrush="Black" BorderThickness="1"
HorizontalAlignment="Left" VerticalAlignment="Top">
<!-- 用 Margin 替代 Canvas.Left/Top -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Border x:Name="TitleBar"
Grid.Row="0"
Style="{StaticResource BorderStyle}"
MouseLeftButtonDown="B_TitleBar_MouseLeftButtonDown"
MouseMove="B_TitleBar_MouseMove">
<TextBlock Text="B的标题栏" Style="{StaticResource TextBlockStyle}"
VerticalAlignment="Center" Margin="5" />
</Border>

<ScrollViewer Grid.Row="1"
VerticalScrollBarVisibility="Auto"
IsHitTestVisible="False">
<ItemsControl x:Name="ContentItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Margin="5" FontSize="14" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Border>
</Grid>
</Window>

xaml.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3Test
{
/// <summary>
///
/// /// MainWindow.xaml 的交互逻辑
/// /// </summary>
public partial class MainWindow : Window
{
private bool _isDragging = false;
private Point _startMousePosition;
private Thickness _startMargin;

public MainWindow()
{
InitializeComponent();
ContentItems.ItemsSource = new List<string> { "项1", "项2", "项3" };
}

private void A_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("点击了组件A");
}

private void B_TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_isDragging)
{
_isDragging = true;
_startMousePosition = e.GetPosition(parentGrid); // 基于 Grid
_startMargin = ComponentB.Margin;
TitleBar.CaptureMouse();
}
else
{
_isDragging = false;
TitleBar.ReleaseMouseCapture();
}

e.Handled = true; // 防止事件冒泡
}

private void B_TitleBar_MouseMove(object sender, MouseEventArgs e)
{
if (_isDragging && TitleBar.IsMouseCaptured)
{
Point currentMousePos = e.GetPosition(parentGrid);

double offsetX = currentMousePos.X - _startMousePosition.X;
double offsetY = currentMousePos.Y - _startMousePosition.Y;

ComponentB.Margin = new Thickness(
_startMargin.Left + offsetX,
_startMargin.Top + offsetY,
0, 0);

}
}
}

}

http://www.gsyq.cn/news/39858.html

相关文章:

  • spring-boot-actuator-Health原理
  • 2025年广州防霉检测机构权威推荐榜单:除螨检测/除臭效果测试/输配水设备检测源头机构精选
  • 体育馆游泳卡押金原路退回,安心无忧—东方仙盟 - 指南
  • 微算法科技(NASDAQ MLGO):以隐私计算区块链筑牢多方安全计算(MPC)安全防线
  • 怎么把idea的目录结构,以文本形式输出?——idea使用tree
  • 微信小程序初始配置
  • 2025年塑烧板除尘器源头厂家权威推荐榜单:耐高温除尘器/防爆除尘器/不锈钢除尘器源头厂家精选
  • 第180天:横向移动篇入口切换SMB共享WMI管道DCOM组件Impacket套件CS插件
  • 利用地名来查询烟台市未来七天天气预报,在jmeter中查看响应结果
  • 高效学习方式——知识关联性
  • 2025年云南做楼体灯光亮化服务商权威推荐榜单:云南做酒店灯光亮化/云南做居民楼灯光亮化/云南做写字楼灯光亮化服务商精选
  • 模板Project10
  • [Python刷题记录]-三数之和-双指针-中等
  • 使用Node.js开发MCP服务器入门总结
  • 2025年菊花种苗生产厂家权威推荐榜单:菊花造型/菊花花坛/菊花种苗基地源头厂家精选
  • go语言获取腾讯股票示例
  • 2025年11月低氮燃烧器品牌前十排名:权威评测与行业洞察
  • 2025年外墙保温装饰一体板厂家推荐:浙江欣阳嘉茂控股集团,保温装饰一体板/保温装饰板/金属保温装饰一体板/薄陶瓷保温装饰一体板/多品类适配建筑需求
  • 如何申请和设置400电话系统?
  • 传图取字小程序:高效 OCR 文字识别工具,轻松实现图片转文字
  • 如何优化智能客服平台的服务流程?技巧分享!
  • Grafana12安装部署
  • 11.4联考总结
  • 维语视频微信小程序系统:一站式视频服务解决方案
  • 永久配置 Gradle 输出编码(彻底解决)
  • 图的dfs和bfs遍历
  • 2025年热风炉品厂家/工厂排名前十:徐州海德测热风炉领跑行业
  • 2025年融资顾问服务内容权威排行榜,上市融资顾问哪个好
  • 2025年列管换热器批发厂家权威推荐榜单:不锈钢列管换热器/冷凝器/列管式冷凝器源头厂家精选
  • 2025 年 11 月强力塑料粉碎机,工业塑料粉碎机,多功能塑料粉碎机厂家最新推荐,实力品牌深度解析采购无忧之选!