我使用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
可能不再工作了。这个想法是否正确?如何以最优雅的方式解决这个问题?
答案 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>
将后者更改为您尝试绑定到的任何父元素的类型。