<Window x:Class="WpfApp132.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:WpfApp132" mc:Ignorable="d" WindowState="Maximized" MouseDown="Window_MouseDown" MouseMove="Window_MouseMove" MouseWheel="Window_MouseWheel" MouseUp="Window_MouseUp" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="200"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <ListBox x:Name="lbx" Grid.Column="0" ItemsSource="{Binding ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectionChanged="lbx_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Width="200" Height="500" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <ContentControl Grid.Column="1" ClipToBounds="True"> <Viewbox x:Name="viewBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <Image x:Name="img" Source="{Binding SelectedItem,ElementName=lbx}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Image.RenderTransform> <TransformGroup> <ScaleTransform x:Name="scaler"/> <TranslateTransform x:Name="translater"/> </TransformGroup> </Image.RenderTransform> </Image> </Viewbox> </ContentControl> </Grid> </Window> //.cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; 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.Navigation; using System.Windows.Shapes; using System.IO; namespace WpfApp132 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); InitImgsList(); this.DataContext = this; } private void InitImgsList() { string fullDir = System.IO.Path.GetFullPath(@"..\..\Images"); if (Directory.Exists(fullDir)) { var files = System.IO.Directory.GetFiles(fullDir, "*", System.IO.SearchOption.AllDirectories); ImgsList = new ObservableCollection<string>(files); lbx.SelectedIndex = 0; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } private ObservableCollection<string> imgsList; public ObservableCollection<string> ImgsList { get { return imgsList; } set { if (value != imgsList) { imgsList = value; OnPropertyChanged(nameof(ImgsList)); } } } private bool isMoving = false; public bool IsMoving { get { return isMoving; } set { if (value != isMoving) { isMoving = value; OnPropertyChanged(nameof(IsMoving)); } } } Point previousPoint; private void Window_MouseDown(object sender, MouseButtonEventArgs e) { previousPoint = e.GetPosition(this); } private void Window_MouseMove(object sender, MouseEventArgs e) { IsMoving = true; } private void Window_MouseWheel(object sender, MouseWheelEventArgs e) { Point pt = e.GetPosition(this); if (e.Delta > 0) { scaler.ScaleX *= 1.2; scaler.ScaleY *= 1.2; } else { scaler.ScaleX /= 1.2; scaler.ScaleY /= 1.2; } scaler.CenterX = pt.X; scaler.CenterY = pt.Y; } private void Window_MouseUp(object sender, MouseButtonEventArgs e) { if (IsMoving && e.ButtonState == MouseButtonState.Released && e.ChangedButton == MouseButton.Left) { Point newPoint = e.GetPosition(this); double deltaX = newPoint.X - previousPoint.X; double deltaY = newPoint.Y - previousPoint.Y; translater.X = deltaX; translater.Y = deltaY; } } private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e) { scaler.ScaleX = 1.0; scaler.ScaleY = 1.0; img.HorizontalAlignment= HorizontalAlignment.Stretch; img.VerticalAlignment= VerticalAlignment.Stretch; } } }
标签:scaler,TranslateTransform,void,System,private,ScaleTransform,Windows,using,WPF From: https://www.cnblogs.com/Fred1987/p/18227715