弹出窗口基于2个条件开放

时间:2016-12-06 09:35:36

标签: c# wpf

我有一个基于Popup(扩展弹出窗口)的Custom UserControl。 现在在这个UserControl中我有这个:

     <Popup.Style>
    <Style TargetType="Popup">
        <Style.Triggers>
            <DataTrigger Binding="{Binding PlacementTarget.IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="OpenPopupStoryBoard" >
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                                <DiscreteBooleanKeyFrame KeyTime="0:0:0.10"  Value="True"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <PauseStoryboard BeginStoryboardName="OpenPopupStoryBoard"/>
                    <BeginStoryboard x:Name="ClosePopupStoryBoard">
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                                <DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="False"/>
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>

            <Trigger Property="IsMouseOver" Value="True">
                <Trigger.EnterActions>
                    <PauseStoryboard BeginStoryboardName="ClosePopupStoryBoard" />
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <PauseStoryboard BeginStoryboardName="OpenPopupStoryBoard"/>
                    <ResumeStoryboard BeginStoryboardName="ClosePopupStoryBoard" />
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</Popup.Style>

所以现在我在另一个UserControl中调用它,基本上弹出窗口打开时用鼠标通过ListItem

<CustomControls:StandardPopup Isopen={Binding Value.Items.Count, Mode=OneWay, Converter={StaticResource ItemsCountToBoolean}, ConverterParameter=0}, PlacementTarget="{Binding ElementName=listboxItem}" Heading="ListItemDetails" >

现在我的问题是,我试图在没有项目的情况下不显示弹出窗口,但是因为已经有IsOpen = MouseOver的触发器,它根据项目的计数不起作用。我该怎么解决这个问题呢。

我也已经在我的CustomUserControl上尝试了CanOpen依赖属性,然后试图绑定到我的StoryBoard上的IsOpen,但显然不能在StoryBoard中使用Binding。

所以我想我正在寻找一个解决方案来设置IsOpen基于触发器和绑定。

2 个答案:

答案 0 :(得分:0)

在此<DataTrigger Binding="{Binding PlacementTarget.IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True">

中使用转换器

<DataTrigger Binding="{Binding PlacementTarget, RelativeSource={RelativeSource Self}, Converter={StaticResource CheckListBoxCnv}}" Value="True">

一个例子可能是:

public class CheckListBoxConverter:IValueConverter { //Check for IsMouseOver property there and do some extra processing, and return true/false value. }

{{1}}

答案 1 :(得分:0)

我不知道你确定是否没有&#34;项目&#34;在Popup中,但是Arie建议您可以使用具有多个条件的MultiDataTrigger。以下是您的示例:

<Style TargetType="Popup">
  <Style.Triggers>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding PlacementTarget.IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True" />
            <!-- add your other condtion here that determines whether there are any items in the Popup, e.g.: -->
            <Condition Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0" />
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.EnterActions>
            <BeginStoryboard x:Name="OpenPopupStoryBoard" >
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                        <DiscreteBooleanKeyFrame KeyTime="0:0:0.10"  Value="True"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </MultiDataTrigger.EnterActions>
        <MultiDataTrigger.ExitActions>
            <PauseStoryboard BeginStoryboardName="OpenPopupStoryBoard"/>
            <BeginStoryboard x:Name="ClosePopupStoryBoard">
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" FillBehavior="HoldEnd">
                        <DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="False"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </MultiDataTrigger.ExitActions>
    </MultiDataTrigger>

    <Trigger Property="IsMouseOver" Value="True">
        <Trigger.EnterActions>
            <PauseStoryboard BeginStoryboardName="ClosePopupStoryBoard" />
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <PauseStoryboard BeginStoryboardName="OpenPopupStoryBoard"/>
            <ResumeStoryboard BeginStoryboardName="ClosePopupStoryBoard" />
        </Trigger.ExitActions>
    </Trigger>
</Style.Triggers>

当然,您必须修改第二个条件以绑定到某个现有属性,该属性确定是否有任何要显示的项目,但您应该明白这一点。