gpt4 book ai didi

c# - 旋转边框不会改变窗口大小

转载 作者:行者123 更新时间:2023-11-30 21:06:41 25 4
gpt4 key购买 nike

我尝试旋转 Border 并让 MainWindow 根据 Border 旋转占用的新空间更改其大小。我已经设置了 SizeToContent="WidthAndHeight" 但是当我旋转边框时窗口大小没有改变。

我是否需要以编程方式为 MainWindow 设置 WidthHeight,或者这可以通过以其他方式更改 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com