您可以使用转换器获得所需的结果:
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>
转换器:
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();
}
}