简体   繁体   English

将缩放值绑定到控件的大小

[英]Binding zoom value to control's size

I have a Canvas with some images (24x24 in size) on it and I also have a scrollbar that zooms in and out the canvas. 我有一个带有一些图像(尺寸为24x24)的Canvas ,我还有一个可以放大和缩小画布的滚动条。

I would like to make the images stay the same size when resizing the canvas so I thought of binding the width / height of the images to the zoom value so that their width / height will be dependant of the zoom value (So, for example, they'll be Width = Width / ZoomValue or something similar) 我想在调整画布大小时使图像保持相同的大小,所以我想将图像的宽度/高度绑定到缩放值,以便它们的宽度/高度将取决于缩放值(例如,它们将是Width = Width / ZoomValue或类似的东西)

How can I obtain this behavior using WPF and binding? 如何使用WPF和绑定获取此行为?

Thanks! 谢谢!

Assuming the images all have a width of 24, bind to ZoomValue and use an IValueConverter: 假设图像的宽度均为24,则绑定到ZoomValue并使用IValueConverter:
(ZoomValue has to be on your ViewModel) (ZoomValue必须在您的ViewModel上)

<Image Source="..." Width="{Binding ZoomValue, Converter={StaticResource ZoomToWidthConverter}" />

And the IValueConverter: 和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;
        }
}

The Resources of the Canvas must hold a key for ZoomToWidthConverter: Canvas的资源必须包含ZoomToWidthConverter的键:

<Canvas>
<Canvas.Resources>
<local:ZoomToWidthConverter x:Key="ZoomToWidthConverter " />
</Canvas.Resources>
</Canvas>

If the images can have different original width, use a MultiBinding along with an IMultiValueConverter. 如果图像可以具有不同的原始宽度,请使用MultiBinding和IMultiValueConverter。

Starting point is to define a converter that you'll use in binding, here are some links that can be useful: 起点是定义一个你将在绑定中使用的转换器,这里有一些有用的链接:

  1. http://www.wpftutorial.net/ValueConverters.html http://www.wpftutorial.net/ValueConverters.html
  2. http://blogs.imeta.co.uk/FMoreno/archive/2008/11/07/498.aspx http://blogs.imeta.co.uk/FMoreno/archive/2008/11/07/498.aspx

Hope this helps! 希望这可以帮助!

Not very sure if there is somethign built-in done on this subject already in WPF 4.0 , also cause this feature was requested long years ago by users. 不太确定在WPF 4.0已经对此主题进行了一些内置功能,也导致用户很久以前就要求使用此功能。 The only valuable solution, imo, that you can apply is to assign your childcontrol.RenderTransform to inverse of the transformation matrix of its parent, so canvas . 您可以应用的唯一有价值的解决方案是将childcontrol.RenderTransform指定为其父级的transformation matrix反转 ,即canvas

Let a databinding to assign correct value 让数据绑定分配正确的值

By doing so, hope it's clear, you reset transformation applied to the canvas and get the "old" matrix. 通过这样做,希望很清楚,您重置应用于画布的转换并获得“旧”矩阵。 Considering that you're talking about a single transformation (there is no Translation , Scale and Rotation made insequence) you have to get mathematically predictable (so correct) "original" matrix (they call it Identity ). 考虑到你在谈论一个单一的转换(没有TranslationScaleRotation使得顺序),你必须得到数学上可预测的(如此正确)“原始”矩阵(他们称之为Identity )。

Look on this answer, which refers to another one again. 看看这个答案,再次引用另一个答案。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM