WPF窗口fadeIn不起作用

时间:2011-05-24 15:53:51

标签: wpf window fadein

我有以下代码:

<Window.Background>
    <SolidColorBrush Opacity="0.7" Color="White" x:Name="BackgroundBrush"></SolidColorBrush>
</Window.Background>
<Window.Triggers>
    <EventTrigger RoutedEvent="Loaded">
        <EventTrigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:5" Storyboard.TargetName="BackgroundBrush" From="0.7">
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.EnterActions>
    </EventTrigger>
</Window.Triggers>

但是当窗口显示时没有任何反应。为什么呢?

2 个答案:

答案 0 :(得分:2)

  1. 您必须为窗口本身的不透明度设置动画,而不是背景。
  2. 您需要将AllowsTransparency设置为true,这也需要将WindowStyle设置为None。 (您需要创建自己的标准窗口按钮)

答案 1 :(得分:1)

除了H.B.说,你需要将BeginStoryboard添加到EventTrigger.Actions集合,而不是EnterActions集合。所以这有效:

<Window.Background>
    <SolidColorBrush Opacity="0.7" Color="White" x:Name="BackgroundBrush"></SolidColorBrush>
</Window.Background>
<Window.Triggers>
    <EventTrigger RoutedEvent="Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:5" Storyboard.TargetName="BackgroundBrush" From="0.7">
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>