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

标签 c# wpf

我尝试旋转 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/

相关文章:

c# - 在 WPF 中使用 Close() 函数与不使用 Close()

c# - 在多个复选框上过滤 ICollectionView

wpf - 如何在 WPF 中为图像添加边框?

c# - 我如何开始构建语音聊天应用程序 C#

c# - 在生成的 EF 类中添加 INotifyPropertyChange

c# - ASP.NET MVC5 以自定义格式显示日期

c# - 在 .net windows 应用程序中使用 log4net

WPF MVVM 依赖属性

c# - 如何设置一个具有两个外键的实体?

C# 如何从拉伸(stretch)位图/图片框获取像素