[英]Border around a rotating image
基本上,我试图做一些事情以使WPF图像位于WPF边框内,并定期通过更改RotateTransform Angle属性来旋转图像。
问题是,当我旋转图像时,边框不会旋转,或尝试更改以适应图片的新形状。 我尝试将其Alignment属性设置为拉伸,甚至将边框的高度/宽度绑定到图像的高度/宽度,但是没有运气。 我怀疑问题是,当我旋转图像时,它实际上并没有改变Image对象的高度或宽度,因此,边框当然不知道要做什么。
有没有更好的方法来旋转图像以允许调整边框的大小,如果没有,考虑到我要更改RotateTransform Angle的情况,如何正确调整边框的大小。
谢谢!
您可以为此使用LayoutTransform而不是RenderTransform。 如果您尝试更改旋转角度,则会看到边框更改大小以适应它。 (想想这就是您的要求吗?如果您实际上想旋转边框,那么可以旋转边框而不是图像)
<Window x:Class="rotate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Border VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="Black" BorderThickness="1">
<Grid Background="Blue" Width="80" Height="80">
<Grid.LayoutTransform>
<RotateTransform Angle="10"/>
</Grid.LayoutTransform>
</Grid>
</Border>
</Grid>
</Window>
使用LayoutTransform
而不是RenderTranform
。
RenderTransform
仅对控件进行可视化转换,并在测量和排列控件后应用。 因此,它不会影响其他控件看到的大小。
LayoutTransform
确实会影响对象的布局。 它是在测量和布置控件之前应用的,因此另一个控件会看到大小的变化。
注意: LayoutTransform
慢得多,通常不会提供平滑的动画。
<Border BorderThickness="5" BorderBrush="Red" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Width="50" Height="50">
<Image.LayoutTransform>
<RotateTransform Angle="45" />
</Image.LayoutTransform>
</Image>
</Border>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.