RenderTransforms 不会改变控件的宽度和高度,因为它只影响渲染。不确定是否有更好的方法,但您可以使用原始大小和比例因子简单地计算渲染大小。
这是一个非常基本的例子
xml
<Canvas x:Name="cnv" Grid.Row="1">
<Rectangle Canvas.Top="150" Canvas.Left="150"
Width="200" Height="200" x:Name="myrect"
Fill="AliceBlue"
ManipulationStarted="myrect_ManipulationStarted"
ManipulationDelta="myrect_ManipulationDelta"
ManipulationCompleted="myrect_ManipulationCompleted">
</Rectangle>
</Canvas>
代码
public MainPage()
{
InitializeComponent();
transformGroup = new TransformGroup();
translation = new TranslateTransform();
scale = new ScaleTransform();
transformGroup.Children.Add(scale);
transformGroup.Children.Add(translation);
myrect.RenderTransform = transformGroup;
}
private TransformGroup transformGroup;
private TranslateTransform translation;
private ScaleTransform scale;
private void myrect_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
//change the color of the Rectangle to a half transparent Red
myrect.Fill = new SolidColorBrush(Color.FromArgb(127, 255, 0, 0));
}
private void myrect_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
translation.X += e.DeltaManipulation.Translation.X;
translation.Y += e.DeltaManipulation.Translation.Y;
//Scale the Rectangle
if (e.DeltaManipulation.Scale.X != 0)
scale.ScaleX *= e.DeltaManipulation.Scale.X;
if (e.DeltaManipulation.Scale.Y != 0)
scale.ScaleY *= e.DeltaManipulation.Scale.Y;
}
private void myrect_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
myrect.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
Debug.WriteLine(myrect.Width * scale.ScaleX);
Debug.WriteLine(myrect.Height * scale.ScaleY);
}