Home > Mobile >  The height and width properties of the WPF window remain zero after running Storyboard
The height and width properties of the WPF window remain zero after running Storyboard

Time:08-24

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.

Output

Best regards,

Reza Jaferi

CodePudding user response:

As is mentioned in this answer, Width/Height is the requested size. In this case, we need to use 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);
  • Related