Window Trigger on Visibility Property in WPF VB

LH63 21 Reputation points
2020-10-05T08:33:57.63+00:00

Hi,

I'm trying to get an animation storyboard to trigger when the Window Visibility is changed. I tried the following code but receive an error i.e. Triggers collection members must be of type EventTrigger. Also tried searching for answers but nothing.

Not sure why I get the error and what I need to do to get the intended result? Any help would be appreciated - Thanks!

Here is my simple test code. It has a Main Window with a Button to make the second BalloonWindow Visible.

Main Window VB code:

Class MainWindow

    Dim myBalloonWindow As New BalloonWindow

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

        myBalloonWindow.Owner = Me

    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

        myBalloonWindow.Visibility = Visibility.Visible

    End Sub

End Class

Main Window XAML:

<Window x:Class="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:BalloonWpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

<Grid>
    <Button Height="40" Width="100" Click="Button_Click">Show Balloon</Button>
</Grid>

</Window>

Balloon Window XAML:

<Window x:Class="BalloonWindow"
        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:BalloonWpfApp"
        mc:Ignorable="d"
        Title="BalloonWindow" AllowsTransparency="True" WindowStyle="None" Background="Transparent" SizeToContent="WidthAndHeight">

    <Window.Triggers>
        <!-- Window Visibility Property Trigger Here e.g. <Trigger Property="Visibility" Value="Visible" /> results in error-->
    </Window.Triggers>

    <Grid Margin="15,18,5,5">

        <Path Stroke="DarkGray" StrokeThickness="1" Fill="LightYellow">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigure StartPoint="20,0" IsClosed="True">
                            <LineSegment Point="35,20"/>
                            <LineSegment Point="285,20"/>
                            <ArcSegment Point="290,25" Size="5,5" SweepDirection="Clockwise"/>
                            <LineSegment Point="290,80"/>
                            <ArcSegment Point="285,85" Size="5,5" SweepDirection="Clockwise"/>
                            <LineSegment Point="5,85"/>
                            <ArcSegment Point="0,80" Size="5,5" SweepDirection="Clockwise"/>
                            <LineSegment Point="0,25"/>
                            <ArcSegment Point="5,20" Size="5,5" SweepDirection="Clockwise"/>
                            <LineSegment Point="20,20"/>
                        </PathFigure>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
            <Path.Effect>
                <DropShadowEffect Color="Black" ShadowDepth="4" Opacity="0.6"></DropShadowEffect>
            </Path.Effect>
        </Path>

    </Grid>

</Window>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-10-06T03:25:19.297+00:00

    Please use below code to replace your Window.Triggers to try:

      <Window.Style>  
            <Style TargetType="Window">  
                <Style.Triggers>  
                    <Trigger Property="Visibility" Value="Visible">  
                        <!--<Setter Property="*****" Value="****"></Setter>-->  
                    </Trigger>  
                </Style.Triggers>  
            </Style>  
        </Window.Style>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful