Caliburn.Micro DataTrigger与RelativeSource TemplatedParent

时间:2017-09-07 07:36:27

标签: wpf xaml mvvm caliburn.micro caliburn

我使用WPF开发了一个不使用特殊MVVM框架的应用程序。现在这个应用程序越来越大;因此,我想切换到Caliburn.Micro,但这会导致一些问题。

我在ListBoxes内有一个包含ItemsControl的视图。在ListBoxItems中放置了一个按钮,一旦用户将鼠标悬停在父ListBox上,就必须更改不透明度,当用户将鼠标悬停在Button上时(以及周围,​​当鼠标再次离开这些控件时)。为实现这一目标,我想出了这个xaml代码:

<Style x:Key="DeleteButtonStyle" TargetType="Button">
    <Setter Property="Opacity" Value="0.0" />
        <Style.Triggers>
            <EventTrigger RoutedEvent="Control.MouseEnter">
                <RemoveStoryboard BeginStoryboardName="PartiallyFadeOutStoryboard" />
                <RemoveStoryboard BeginStoryboardName="FadeOutStoryboard" />
                <BeginStoryboard Storyboard="{StaticResource FadeInStoryboard}" x:Name="FadeInStoryboard"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="Control.MouseLeave">
                <RemoveStoryboard BeginStoryboardName="PartiallyFadeInStoryboard" />
                <RemoveStoryboard BeginStoryboardName="FadeInStoryboard" />
                <BeginStoryboard Storyboard="{StaticResource PartiallyFadeOutStoryboard}" x:Name="PartiallyFadeOutStoryboard"/>
            </EventTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver, NotifyOnSourceUpdated=True}" Value="True">
                <DataTrigger.EnterActions>
                    <RemoveStoryboard BeginStoryboardName="PartiallyFadeOutStoryboard" />
                    <RemoveStoryboard BeginStoryboardName="FadeOutStoryboard" />
                    <BeginStoryboard Storyboard="{StaticResource PartiallyFadeInStoryboard}" x:Name="PartiallyFadeInStoryboard"/>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <RemoveStoryboard BeginStoryboardName="PartiallyFadeInStoryboard" />
                    <RemoveStoryboard BeginStoryboardName="FadeInStoryboard" />
                    <RemoveStoryboard BeginStoryboardName="PartiallyFadeOutStoryboard" />
                    <BeginStoryboard Storyboard="{StaticResource FadeOutStoryboard}" x:Name="FadeOutStoryboard"/>
                 </DataTrigger.ExitActions>
              </DataTrigger>
          </Style.Triggers>
      </Style>

可悲的是,在我切换到Caliburn之后,现在已经没有了,因为最后DataTrigger的代码不再执行了。我认为这种行为的原因是我之前在DataTemplate中使用ItemsControl来定义视图;但现在我创建了一个额外的.xaml文件/类,仅用于绑定到ItemsControl(包含ListBox)的项目的视图 - 因此,RelativeSource TemplatedParent可能不再工作了。这个想法是否正确?如何以最优雅的方式解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试将{RelativeSource}AncestorType

一起使用
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsMouseOver}" Value="True">
    <DataTrigger.EnterActions>
        <RemoveStoryboard BeginStoryboardName="PartiallyFadeOutStoryboard" />
        <RemoveStoryboard BeginStoryboardName="FadeOutStoryboard" />
        <BeginStoryboard Storyboard="{StaticResource PartiallyFadeInStoryboard}" x:Name="PartiallyFadeInStoryboard"/>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <RemoveStoryboard BeginStoryboardName="PartiallyFadeInStoryboard" />
        <RemoveStoryboard BeginStoryboardName="FadeInStoryboard" />
        <RemoveStoryboard BeginStoryboardName="PartiallyFadeOutStoryboard" />
        <BeginStoryboard Storyboard="{StaticResource FadeOutStoryboard}" x:Name="FadeOutStoryboard"/>
    </DataTrigger.ExitActions>
</DataTrigger>

将后者更改为您尝试绑定到的任何父元素的类型。