简体   繁体   English

WPF RenderTransform

[英]WPF RenderTransform

I am drawing an Ellipse in the center of a Canvas . 我正在Canvas的中心绘制一个Ellipse When doing so the top left corner of the Ellipse is actually drawn in the center but I want the center point of the Ellipse to be in the center. 这样做时, Ellipse左上角实际上是绘制在中心,但是我希望Ellipse的中心在中心。 For this I presume I need a RenderTransform but I cannot get it working correctly. 为此,我想我需要一个RenderTransform但是我无法使其正常工作。

<Ellipse Stroke="Red" RenderTransformOrigin="0.5,0.5" StrokeThickness="5" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100">

Am I missing an attribute? 我是否缺少属性?

Basically you cannot do this through attached properties unless you bind through viewModel which knows the exact center. 基本上,除非您通过知道确切中心的viewModel进行绑定,否则您无法通过附加属性来执行此操作。 You will need to use code behind to find out the center of canvas. 您将需要使用后面的代码来找出画布的中心。 Like: 喜欢:

myCanvas.SetLeft(elipse, (myCanvas.ActualWidth - elipse.ActualWidth) / 2);
myCanvas.SetTop(elipse, (myCanvas.ActualHeight - elipse.ActualHeight) / 2);

You can achieve the desired result using a converter: 您可以使用转换器获得所需的结果:

Xaml: Xaml:

<Window.Resources>
    <wpfApplication1:GetCentreConverter x:Key="CentreConverter"/>
</Window.Resources>
<Canvas x:Name="canvas">
    <Ellipse Stroke="Red"
             RenderTransformOrigin="0.5,0.5"
             StrokeThickness="5"
             Width="100"
             Height="100"
             Canvas.Left="{Binding ElementName=canvas, Path=ActualWidth, Converter={StaticResource CentreConverter}, ConverterParameter=50}"
             Canvas.Top="{Binding ElementName=canvas, Path=ActualHeight, Converter={StaticResource CentreConverter}, ConverterParameter=50}"/>
</Canvas>

Converter: 转换器:

public class GetCentreConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var width = double.Parse(value.ToString());
        var offset = double.Parse(parameter.ToString());

        return width/2 - offset;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You could use a Grid instead of Canvas as the container of the ellipse. 您可以使用Grid而不是Canvas作为椭圆的容器。 So with 所以用

HorizontalAlignment="Center" VerticalAlignment="Center"

you will get your ellipse in the center of the Grid positioned. 您将使椭圆位于Grid的中心位置。

You need to use the Canvas.Left and Canvas.Top attached properties, as Vidas stated. 正如Vidas所述,您需要使用Canvas.LeftCanvas.Top附加属性。

In addition to doing it in the code-behind, you could also write a custom converter so you could do it in XAML. 除了在后面的代码中进行操作之外,您还可以编写一个自定义转换器,以便可以在XAML中进行操作。 For instance, if you write a DivideByTwoConverter, you could then bind the attached properties to the Width and Height of the Canvas and use the converter on the bindings. 例如,如果您编写DivideByTwoConverter,则可以将附加属性绑定到Canvas的Width和Height,并在绑定上使用转换器。

I'm sure there are some other tricks to do the same. 我敢肯定,还有其他一些技巧可以做到这一点。

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

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