当前位置: 首页 > 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.jsqmd.com/news/31288/

相关文章:

  • 关于嵌入式硬件需要了解的基础知识 - 教程
  • spring-boot-actuator-Health原理
  • 2025年广州防霉检测机构权威推荐榜单:除螨检测/除臭效果测试/输配水设备检测源头机构精选
  • 2025年钣金机箱外壳加工厂家权威推荐榜单:钣金壳体/变频器钣金壳体/钣金加工壳体源头厂家精选
  • 从零到一:我的开源AI商业化实战之路
  • 机器学习 BASEML到底是什么
  • 体育馆游泳卡押金原路退回,安心无忧—东方仙盟 - 指南
  • DesignSpark Mechanical (DSM)输入用户名密码提示在注册过程中发生错误
  • 字段(辨析:字段、对象、属性和方法在 JavaScript 中的关系)
  • P14364 [CSP-S 2025] 员工招聘 / employ 笔记
  • Spring boot 使用虚拟线程示例
  • 微算法科技(NASDAQ MLGO):以隐私计算区块链筑牢多方安全计算(MPC)安全防线
  • 怎么把idea的目录结构,以文本形式输出?——idea使用tree
  • 2025年11月沼气直燃厂家综合评测:徐州海德测控技术有限公司领跑
  • 微信小程序初始配置
  • python爬虫scrapy框架使用 - 教程
  • 2025年塑烧板除尘器源头厂家权威推荐榜单:耐高温除尘器/防爆除尘器/不锈钢除尘器源头厂家精选
  • 2025年剪叉升降平台供应商权威推荐榜单:车载剪叉式升降平台/移动剪叉式升降平台车/轨道升降平台源头厂家精选
  • 第180天:横向移动篇入口切换SMB共享WMI管道DCOM组件Impacket套件CS插件
  • 2025年11月沼气直燃品牌/品牌排名前十:技术实力对比与总结
  • 高效学习方式——知识关联性
  • 基于时间的ACL - 教程
  • 2025年云南做楼体灯光亮化服务商权威推荐榜单:云南做酒店灯光亮化/云南做居民楼灯光亮化/云南做写字楼灯光亮化服务商精选
  • 信息系统方案的范围管理(12345智慧政务)
  • [Python刷题记录]-三数之和-双指针-中等
  • Serilog日志库简单实践(一):控制台与调试Sinks(.NET 8)汇报总结
  • WPF实现组件拖动(Canvas)
  • 2025 年最新测控终端厂家推荐:符合国标 + 数据透传技术,靠谱企业深度测评报告4G 测控终端/远程测控终端/物联网测控终端/测控终端 RTU 公司推荐
  • 使用Node.js开发MCP服务器入门总结
  • 2025年菊花种苗生产厂家权威推荐榜单:菊花造型/菊花花坛/菊花种苗基地源头厂家精选