C# 由DataTrigger触发的弹出打开

C# 由DataTrigger触发的弹出打开,c#,wpf,xaml,popup,datatrigger,C#,Wpf,Xaml,Popup,Datatrigger,我正在尝试根据ViewModel中的数据更改打开弹出窗口: 弹出窗口的定义如下: <Popup x:Name="popup" AllowsTransparency="True" Focusable="False" IsHitTestVisible="False" Placement="Bottom" PopupAnimation="Slide" StaysOpen="False">... </Popup> 。。。 我有用户控制 <UserControl>

我正在尝试根据ViewModel中的数据更改打开弹出窗口:

弹出窗口的定义如下:

 <Popup x:Name="popup"
AllowsTransparency="True"
Focusable="False"
IsHitTestVisible="False"
Placement="Bottom"
PopupAnimation="Slide"
StaysOpen="False">... </Popup>
。。。
我有用户控制

<UserControl>
 ...
<ControlTemplate>
  <ControlTemplate.Triggers>
     <DataTrigger Binding="{Binding PopupOpened}" Value="True">
         <DataTrigger.EnterActions>
             <BeginStoryboard Storyboard="{StaticResource ShowPopup}"/>
         </DataTrigger.EnterActions>
     </DataTrigger>
  </ControlTemplate.Triggers>
</ControlTemplate>
</UserControl>

...
我的动画定义为(在参考资料中):


如果我从某个控件的触发器(例如按钮)上的EventTrigger触发此动画,它将正常工作

它在DataTrigger中无法正常工作

编辑:

我发现了一个问题:
如果使用动画+绑定控制Popup.IsOpen属性,则只有在使用动画更改该属性之前,绑定到该属性才有效。之后,绑定将不再工作。因此,您必须始终通过动画或绑定而不是混合来更改等参线特性

您的示例中有几个错误掩盖了真正的问题。 您的
DataTrigger
元素未关闭,“StaticResources”应该是
StaticResource

但最终,UserControl的模板没有正确使用,您错过了
UserControl.template
设置。如果我们修复它,我们将看到异常:

<UserControl>
    <UserControl.Template>
        <ControlTemplate>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding PopupOpened}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource ShowPopup}" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </ControlTemplate.Triggers>
            <!-- Some content for template to have substance -->
            <TextBlock />
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

结果如下:

在的名称范围中找不到“popup”名称 “System.Windows.Controls.ControlTemplate”

实际上,IMO最简单的方法是简单的直接绑定:

<Popup x:Name="popup"
       IsOpen="{Binding PopupOpened, Mode=TwoWay}">

或者,如果确实需要触发器,可以将其移动到弹出窗口:

<Popup x:Name="popup">
    <Popup.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding PopupOpened}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00.00" Value="True" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Popup.Style>

    <TextBlock />
</Popup>

您的示例中有几个错误掩盖了真正的问题。 您的
DataTrigger
元素未关闭,“StaticResources”应该是
StaticResource

但最终,UserControl的模板没有正确使用,您错过了
UserControl.template
设置。如果我们修复它,我们将看到异常:

<UserControl>
    <UserControl.Template>
        <ControlTemplate>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding PopupOpened}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource ShowPopup}" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </ControlTemplate.Triggers>
            <!-- Some content for template to have substance -->
            <TextBlock />
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

结果如下:

在的名称范围中找不到“popup”名称 “System.Windows.Controls.ControlTemplate”

实际上,IMO最简单的方法是简单的直接绑定:

<Popup x:Name="popup"
       IsOpen="{Binding PopupOpened, Mode=TwoWay}">

或者,如果确实需要触发器,可以将其移动到弹出窗口:

<Popup x:Name="popup">
    <Popup.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding PopupOpened}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00.00" Value="True" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Popup.Style>

    <TextBlock />
</Popup>