虽然它并不漂亮,但这里有一个使用RenderTransform
不同来源的嵌套 s 来偏移对象的位置以使其居中的示例:
<UserControl x:Class="UrlTest.Center"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
<ContentControl Canvas.Top="100" Content="{Binding ActualWidth,ElementName=Center, StringFormat=Width \{0\}}"/>
<ContentControl Canvas.Top="120" Content="{Binding ActualHeight,ElementName=Center, StringFormat=Height \{0\}}"/>
<Grid x:Name="Center" RenderTransformOrigin="-.5,-.5" Canvas.Left="40" Canvas.Top="25">
<Grid.RenderTransform>
<ScaleTransform ScaleX="-1" ScaleY="-1"/>
</Grid.RenderTransform>
<Grid RenderTransformOrigin="-.25,-.25">
<Grid.RenderTransform>
<ScaleTransform ScaleX="-1" ScaleY="-1"/>
</Grid.RenderTransform>
<Ellipse Width="80" Height="50" Fill="Aquamarine"/>
<ContentControl FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">Center</ContentControl>
</Grid>
</Grid>
</Canvas>
</UserControl>
内部Grid
s 用于创建转换,而外部 s 用于Canvas
确保其Grid
内部将其大小设置为内部内容的大小。