WPF TranslateTransform +=deltaX +=deltaY
<Window x:Class="WpfApp136.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:WpfApp136" MouseDown="img_MouseDown" MouseUp="img_MouseUp" MouseMove="img_MouseMove" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="200"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <ListBox x:Name="lbx" Grid.Column="0" SelectionChanged="lbx_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Width="200" Height="500" RenderOptions.BitmapScalingMode="LowQuality" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Image Grid.Column="1" x:Name="img" ClipToBounds="True" Source="{Binding SelectedItem,ElementName=lbx,Mode=TwoWay}" RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" MouseDown="img_MouseDown" MouseMove="img_MouseMove" MouseUp="img_MouseUp"> <Image.RenderTransform> <TranslateTransform x:Name="translater"/> </Image.RenderTransform> </Image> </Grid> </Window> // using System; using System.Collections.Generic; 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.Navigation; using System.Windows.Shapes; using System.IO; namespace WpfApp136 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var files = Directory.GetFiles(@"..\..\Images"); lbx.ItemsSource = files; } Point prePoint { get; set; } bool isMoving { get; set; } = false; private void img_MouseDown(object sender, MouseButtonEventArgs e) { prePoint = e.GetPosition(this); } private void img_MouseMove(object sender, MouseEventArgs e) { isMoving = true; } private void img_MouseUp(object sender, MouseButtonEventArgs e) { if(isMoving && e.ChangedButton== MouseButton.Left && e.ButtonState==MouseButtonState.Released) { Point pt = e.GetPosition(this); translater.X += pt.X - prePoint.X; translater.Y += pt.Y - prePoint.Y; isMoving = false; } } private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e) { translater.X = 0; translater.Y=0; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2023-06-05 cpp test write content speed to ssd and usual disk respectively 1M,10M,100M rows data,the fact has illustrated the write speed of ssd is 4-5 times faster than usual disk
2023-06-05 c++ condition_variable wait unique_lock,cv.notifyall()
2023-06-05 c++ tree data structure
2020-06-05 C# deflatestream compress and decompress, compare their respective md5
2020-06-05 C# BinaryWriter BinaryReader demo
2019-06-05 C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小