Caliburn.Micro 带有 RelativeSource TemplatedParent 的 DataTrigger

Caliburn.Micro DataTrigger with RelativeSource TemplatedParent

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

我有一个 ItemsControl 内包含 ListBoxes 的视图。在 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 的代码不再执行。我认为这种行为的原因是我之前在 ItemsControl 中使用了 DataTemplate 来定义视图;但现在我创建了一个额外的 .xaml file/class 只是为了查看绑定到 ItemsControl(其中包含 ListBox)的项目 - 因此,RelativeSource TemplatedParent 可能不再工作了。这个想法对吗?如何以最优雅的方式解决这个问题?

尝试使用 {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>

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