WPF 渲染变换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17489615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF RenderTransform
提问by Chris
I am drawing an Ellipsein the center of a Canvas. When doing so the top left corner of the Ellipseis actually drawn in the center but I want the center point of the Ellipseto be in the center. For this I presume I need a RenderTransformbut I cannot get it working correctly.
我Ellipse在 a 的中心绘制 an Canvas。这样做时, 的左上角Ellipse实际上是在中心绘制的,但我希望 的中心点Ellipse在中心。为此,我认为我需要一个,RenderTransform但我无法使其正常工作。
<Ellipse Stroke="Red" RenderTransformOrigin="0.5,0.5" StrokeThickness="5" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="100">
Am I missing an attribute?
我错过了一个属性吗?
回答by Vidas Vasiliauskas
Basically you cannot do this through attached properties unless you bind through viewModel which knows the exact center. You will need to use code behind to find out the center of canvas. Like:
基本上你不能通过附加属性来做到这一点,除非你通过知道确切中心的 viewModel 进行绑定。您将需要使用后面的代码来找出画布的中心。喜欢:
myCanvas.SetLeft(elipse, (myCanvas.ActualWidth - elipse.ActualWidth) / 2);
myCanvas.SetTop(elipse, (myCanvas.ActualHeight - elipse.ActualHeight) / 2);
回答by Richard E
You can achieve the desired result using a converter:
您可以使用转换器获得所需的结果:
Xaml:
Xml:
<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();
}
}
回答by Dong
You could use a Gridinstead of Canvasas the container of the ellipse. So with
您可以使用 aGrid而不是Canvas作为椭圆的容器。所以与
HorizontalAlignment="Center" VerticalAlignment="Center"
you will get your ellipse in the center of the Gridpositioned.
你会得到你的椭圆在Grid定位的中心。
回答by Tim
You need to use the Canvas.Leftand Canvas.Topattached properties, as Vidas stated.
正如 Vidas 所说,您需要使用Canvas.Left和Canvas.Top附加属性。
In addition to doing it in the code-behind, you could also write a custom converter so you could do it in 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.
除了在代码隐藏中执行此操作外,您还可以编写自定义转换器,以便在 XAML 中执行此操作。例如,如果您编写一个 DivideByTwoConverter,那么您可以将附加属性绑定到 Canvas 的 Width 和 Height 并在绑定上使用转换器。
I'm sure there are some other tricks to do the same.
我相信还有其他一些技巧可以做到这一点。

