//xaml
<Window x:Class="WpfApp104.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:WpfApp104"
mc:Ignorable="d" WindowState="Maximized"
Title="{Binding ScaleValueStr}" Height="450" Width="800">
<Grid>
<Canvas x:Name="imgContainer">
<Canvas.LayoutTransform>
<ScaleTransform ScaleX="{Binding ScaleValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ScaleY="{Binding ScaleValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</Canvas.LayoutTransform>
<Image Source="{Binding ImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
MouseRightButtonDown="imgContainer_MouseRightButtonDown" Stretch="Uniform"
MouseLeftButtonDown="Image_MouseLeftButtonDown"
MouseWheel="imgContainer_MouseWheel" />
</Canvas>
<TextBlock Text="{Binding ScaleValueStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="30" />
</Grid>
</Window>
//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
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;
namespace WpfApp104
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private double scaleValue = 1.0;
public double ScaleValue
{
get
{
return scaleValue;
}
set
{
if (scaleValue != value)
{
scaleValue = value;
OnPropertyChanged(nameof(ScaleValue));
ScaleValueStr = Math.Round(ScaleValue, 2).ToString("0.00");
}
}
}
private string scaleValueStr = "1.00";
public string ScaleValueStr
{
get
{
return scaleValueStr;
}
set
{
if(value!= scaleValueStr)
{
scaleValueStr = value;
OnPropertyChanged(nameof(ScaleValueStr));
}
}
}
private string imgSource = string.Empty;
public string ImgSource
{
get
{
return imgSource;
}
set
{
if(value!= imgSource)
{
imgSource = value;
OnPropertyChanged(nameof(ImgSource));
}
}
}
private static List<string> ImgsList = new List<string>();
public MainWindow()
{
InitializeComponent();
this.DataContext= this;
InitImgsList();
ImgSource = ImgsList[0];
}
private void InitImgsList()
{
string fullDir = System.IO.Path.GetFullPath(@"..\..\Images");
ImgsList.AddRange(Directory.GetFiles(fullDir));
}
private void imgContainer_MouseWheel(object sender, MouseWheelEventArgs e)
{
if(e.Delta>0)
{
ScaleValue *= 1.2;
}
else
{
ScaleValue /= 1.2;
}
}
private int currentIndex = 0;
private void imgContainer_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (currentIndex<0 || currentIndex+1>=ImgsList.Count)
{
currentIndex = 0;
}
ImgSource = ImgsList[++currentIndex];
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (currentIndex-1<0)
{
currentIndex=ImgsList.Count-1;
}
ImgSource = ImgsList[--currentIndex];
}
}
}
![]()
![]()
![]()