WPF:调整圆的大小,保持中心点而不是左上角? [英] WPF: Resizing a circle, keeping the center point instead of TopLeft?

查看:26
本文介绍了WPF:调整圆的大小,保持中心点而不是左上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想借助滑块调整画布上的圆圈大小.这个圆圈可以通过我在后面的代码中做的一些拖放操作在画布上移动,所以它的位置是不固定的.

I'd like to resize a circle on my canvas with the help of a slider. This circle can be moved around on the canvas by some drag&drop stuff I did in code behind, so its position is not fixed.

我已将滑块的值绑定到椭圆的高度和宽度.不幸的是,当我使用滑块时,圆的左上点(实际上是它所在矩形的左上点)在操作期间保持不变.

I have bound the slider's value to an ellipse's height and width. Unfortunately, when I use the slider, the circle gets resized with its top left point (actually the top left point of the rectangle it's sitting in) staying the same during the operation.

我想调整它的大小,使其中心点在操作过程中保持不变.在 XAML 中有没有一种简单的方法可以做到这一点?顺便说一句,我已经尝试过 ScaleTransform,但它并没有达到我想要的效果.

I would like to resize it with its center point being constant during the operation. Is there an easy way to do this in XAML? BTW, I already tried ScaleTransform, but it didn't quite do what I wanted.

非常感谢!:-)

一月

<Canvas x:Name="MyCanvas">

    <!-- this is needed for some adorner stuff I do in code behind -->
    <AdornerDecorator Canvas.Left="10"
                      Canvas.Top="10">
        <Ellipse x:Name="myEllipse"
             Height="{Binding Path=Value, ElementName=mySlider}"
             Width="{Binding Path=Value, ElementName=mySlider}"
             Stroke="Aquamarine"
             Fill="AliceBlue"
             RenderTransformOrigin="0.5 0.5">
            <Ellipse.RenderTransform>
                <RotateTransform Angle="{Binding Path=Value, ElementName=myRotationSlider}" />
            </Ellipse.RenderTransform>
        </Ellipse>
    </AdornerDecorator>

    <Slider x:Name="mySlider"
            Maximum="100"
            Minimum="0"
            Width="100"
            Value="10"
            Canvas.Left="150"
            Canvas.Top="10" />
    <Slider x:Name="myRotationSlider"
            Maximum="360"
            Minimum="0"
            Width="100"
            Value="0"
            Canvas.Left="150"
            Canvas.Top="50" />
</Canvas>

推荐答案

您可以通过 ValueConverter 将 Canvas.Left 和 Canvas.Top 绑定到您的高度和宽度.

You can bind your Canvas.Left and Canvas.Top to your Height and Width via a ValueConverter.

特别(编辑):
为 Canvas.Left 和 Canvas.Top 分别创建一个属性并绑定到这些.
存储宽度和高度的旧值或旧滑块值.
每当滑块发生变化时,通过减去存储的值来获得增量变化dx".
(不要忘记更新存储的值...)
将 dx 添加到 Width 和 Height 属性.
并且,正如 Will 所说,将 dx/2*-1 添加到 Canvas.Left 和 Canvas.Top 属性.

Specifically (edit):
Create a property each for the Canvas.Left and Canvas.Top and bind to these.
Store the old values for Width and Heigth or the old slider value.
Whenever the slider is changed, get the incremental change "dx" by subtracting the stored value.
(Don't forget to update the stored value...)
Add dx to Width and Height property.
And, as Will said, add dx/2*-1 to Canvas.Left and Canvas.Top properties.

有意义吗?

这篇关于WPF:调整圆的大小,保持中心点而不是左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆