我尝试旋转 Border
并让 MainWindow
根据 Border
旋转占用的新空间更改其大小。
我已设置 SizeToContent="WidthAndHeight"
,但旋转边框时窗口大小不会发生变化。
我是否需要以编程方式设置主窗口的宽度
和高度
,或者可以通过其他方式更改xaml代码来实现?
我的xaml代码:
<Window x:Class="MyClass.MainWindow"
WindowStyle="None" AllowsTransparency='True'
Topmost='False' Background="Transparent" ShowInTaskbar='False'
SizeToContent="WidthAndHeight" WindowStartupLocation="Manual">
<Border Name="MyBorder"
BorderBrush="Transparent"
Background="Transparent"
HorizontalAlignment="Left"
VerticalAlignment="Top"
RenderTransformOrigin="0.5,0.5">
</Border>
</Windows>
我在 Window_KeyDown
上的 c#
代码:
# RotateTransform rt = new RotateTransform()
在类级别声明。
if (e.Key == Key.I)
{
if (rt.Angle + 1 < 360)
{
rt.Angle += 1;
}
else
{
rt.Angle = 0;
}
MyBorder.RenderTransform = rt;
}
最佳答案
使用LayoutTransform
而不是RenderTransform
来自 MSDN:Transforms Overview
LayoutTransform – A transform that is applied before the layout pass. After the transform is applied, the layout system processes the transformed size and position of the element.
RenderTransform – A transform that modifies the appearance of the element but is applied after the layout pass is complete. By using the RenderTransform property instead of the LayoutTransform property, you can obtain performance benefits.
示例
<Border Name="MyBorder"
BorderBrush="Transparent"
Background="Transparent"
HorizontalAlignment="Left"
VerticalAlignment="Top"
RenderTransformOrigin="0.5,0.5">
<Border.LayoutTransform>
<RotateTransform Angle="90"/>
</Border.LayoutTransform>
</Border>
所以在你的情况
RotateTransform rt = new RotateTransform(0.0, 0.5, 0.5);
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.I)
{
if (rt.Angle + 1 < 360)
{
rt.Angle += 1;
}
else
{
rt.Angle = 0;
}
MyBorder.LayoutTransform = rt;
}
}}
关于c# - 旋转边框不会改变窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978751/