假设所有图片的宽度都为24,请绑定ZoomValue并使用IValueConverter:
(ZoomValue必须在您的ViewModel上)
<Image Source="..." Width="{Binding ZoomValue, Converter={StaticResource ZoomToWidthConverter}" />
还有IValueConverter:
public class ZoomToWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Double imgOriginalWidth = 24;
return imgOriginalWidth / System.Convert.ToDouble(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Double imgOriginalWidth = 24;
return System.Convert.ToDouble(value) * imgOriginalWidth;
}
}
< p > Canvas的资源必须包含ZoomToWidthConverter的密钥:
<Canvas>
<Canvas.Resources>
<local:ZoomToWidthConverter x:Key="ZoomToWidthConverter " />
</Canvas.Resources>
</Canvas>
如果图像具有不同的原始宽度,请使用MultiBinding和IMultiValueConverter。
ScaleTransform
,它将绑定到缩放值并为1 / ZoomValue
。 - Jonathan Perry