WPF:调整了一圈,保持左上的中心点呢? [英] WPF: Resizing a circle, keeping the center point instead of TopLeft?

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

问题描述

我想调整我的画布上一圈采用了滑盖的帮助。这个圈子可以移动画布一些阻力和放大器上;下降的东西,我在code做后面,所以它的位置不是固定的。

我已绑定了滑块的值椭圆的高度和宽度。不幸的是,当我使用滑块,圆被调整大小与它的左上角点(实际上左上点它坐在矩形)的操作过程中保持不变。

我想用它的中心点是在操作过程中不断调整其大小。有一个简单的方法来做到这在XAML?顺便说一句,我已经尝试过ScaleTransform,但它并没有完全做到我想要的。

感谢一大堆! : - )

1

 <帆布X:名称=MyCanvas>

    <! - 这是需要一些装饰器的东西我在后面code  - >
    &所述; AdornerDecorator Canvas.Left =10
                      Canvas.Top =10>
        <椭圆X:名称=myEllipse
             身高={绑定路径=价值的ElementName = mySlider}
             WIDTH ={绑定路径=价值的ElementName = mySlider}
             行程=海蓝宝石
             填写=艾莉斯蓝
             RenderTransformOrigin =0.5 0.5>
            < Ellipse.RenderTransform>
                < RotateTransform角={绑定路径=价值的ElementName = myRotationSlider}/>
            < /Ellipse.RenderTransform>
        < /椭圆>
    < / AdornerDecorator>

    <滑块X:名称=mySlider
            最大=100
            最小=0
            WIDTH =100
            值=10
            Canvas.Left =150
            Canvas.Top =10/>
    <滑块X:名称=myRotationSlider
            最大=360
            最小=0
            WIDTH =100
            值=0
            Canvas.Left =150
            Canvas.Top =50/>
< /帆布>
 

解决方案

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

具体(编辑):
创建一个每个为Canvas.Left和Canvas.Top并绑定到这些属性。
存储旧值宽度和Heigth或旧的滑块值。
每当滑块改变时,通过减去存储的值获得增量变化DX。
(不要忘了更新存储的值...)
加入DX为宽度和高度属性。
而且,正如威尔说,加入DX / 2 * -1到Canvas.Left和Canvas.Top属性。

这是否有道理?

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.

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.

Thanks a bunch! :-)

Jan

<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>

解决方案

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

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.

Does that make sense?

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

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