<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; } } }
标签:TranslateTransform,void,object,System,deltaX,Windows,using,WPF,MainWindow From: https://www.cnblogs.com/Fred1987/p/18233508