XAML 运行情节提要后,WPF窗口的height和width属性仍为零

relj7zay  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(244)

In order for my app's storyboard to work properly, I must divide the window's height and width by two and then use those values as the CenterY and CenterX values.
XAML :

<Window x:Name="GanjAsemanMainWindow" x:Class="Ganj_Aseman.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ganj_Aseman"
        mc:Ignorable="d"
        Loaded="GanjAsemanMainWindow_Loaded" AllowsTransparency="True" Background="Transparent" FontSize="13" Height="535" ResizeMode="CanResizeWithGrip" Title="MainWindow" Width="764" WindowStyle="None">
    <Window.RenderTransform>
        <RotateTransform CenterX="{Binding}" CenterY="{Binding}" Angle="{Binding}"/>
    </Window.RenderTransform>
    <Window.Resources>
        <Storyboard x:Key="WindowRotation">
            <DoubleAnimation BeginTime="00:00:00" SpeedRatio="2.5" Duration="00:00:2" From="{Binding Path=Height,ElementName=GanjAsemanMainWindow}" To="0" AutoReverse="False" Storyboard.TargetName="GanjAsemanMainWindow" Storyboard.TargetProperty="Height"/>
            <DoubleAnimation BeginTime="00:00:00" SpeedRatio="2.5" Duration="00:00:2" From="{Binding Path=Width,ElementName=GanjAsemanMainWindow}" To="0" AutoReverse="False" Storyboard.TargetName="GanjAsemanMainWindow" Storyboard.TargetProperty="Width"/>
            <DoubleAnimation BeginTime="00:00:00" SpeedRatio="3.5" Duration="00:00:2" From="0" To="360" AutoReverse="False" Storyboard.TargetName="GanjAsemanMainWindow" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
        </Storyboard>
    </Window.Resources>
    <Grid>
        <Image x:Name="MinimizeIconImage" PreviewMouseLeftButtonDown="MinimizeIconImage_PreviewMouseLeftButtonDown" MouseEnter="MinimizeIconImage_MouseEnter" MouseLeave="MinimizeIconImage_MouseLeave" GotFocus="MinimizeIconImage_GotFocus" LostFocus="MinimizeIconImage_LostFocus" Focusable="True" FocusVisualStyle="{x:Null}" Cursor="Hand" Height="47"  VerticalAlignment="Top" Source="{Binding}" Margin="0,2,67,0" HorizontalAlignment="Right" Width="52"/>
    </Grid>
</Window>

C# :

private void MinimizeIconImage_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    //I checked the height and width values after resizing using "Grip" here
    RotateTransform RT = new RotateTransform(0, Width/2 ,Height/2);
    GanjAsemanMainWindow.RenderTransform = RT;
    (Resources["WindowRotation"] as Storyboard).Begin();
}

When the storyboard first runs and the height and width values become zero, I resize the window using the Grip icon, but the values of those two properties do not change and remain zero.

Best regards,
Reza Jaferi

j7dteeu8

j7dteeu81#

正如在answer中提到的,Width/Height是请求的大小。在本例中,我们需要使用ActualWidth/ActualHeight
XAML文件:

From="{Binding Path=ActualHeight,ElementName=GanjAsemanMainWindow}"
From="{Binding Path=ActualWidth,ElementName=GanjAsemanMainWindow}"

C#:

RotateTransform RT = new RotateTransform(0, ActualWidth / 2, ActualHeight / 2);

相关问题