我有一个网格,当它用鼠标悬停时,会使其中一个子元素变为生命(不透明度从0到1),但该网格也可以被处理掉(该网格是列表框的一部分,可以通过网格上的关闭按钮删除。
当用户单击“删除”按钮时,它还会启动MouseLeave事件,在该事件发生之后,该事件当然无法再找到我的DockStackPanel控件。我该如何解决这个问题?
<Grid.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetName="DockStackPanel" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.5" Storyboard.TargetName="DockStackPanel" Storyboard.TargetProperty="Opacity" To="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
答案 0 :(得分:2)
您是否尝试过在代码中创建动画。我在这里假设DockStackPanel控件是一个StackPanel。
public MainWindow()
{
InitializeComponent();
MyGrid.MouseEnter += MyGrid_MouseEnter;
MyGrid.MouseLeave += MyGrid_MouseLeave;
}
void MyGrid_MouseLeave(object sender, MouseEventArgs e)
{
if (DockStackPanel != null)
{
var dur = new Duration(new TimeSpan(0, 0, 0, 0, 500));
var anim = new DoubleAnimation(0, dur);
DockStackPanel.BeginAnimation(StackPanel.OpacityProperty, anim);
}
}
void MyGrid_MouseEnter(object sender, MouseEventArgs e)
{
var dur = new Duration(new TimeSpan(0, 0, 0, 0, 500));
var anim = new DoubleAnimation(1, dur);
DockStackPanel.BeginAnimation(StackPanel.OpacityProperty, anim);
}
答案 1 :(得分:1)
您可以做的另一件事是为DataTrigger
实施Grid
,它将覆盖默认的MouseLeave
和MouseEnter
事件,以便在DisableTriggers
属性更改时不执行任何操作。
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger Binding="{Binding DisableTriggers}" Value="True">
<Setter Property="Style" Value="{StaticResource GridStyleWithoutStoryboards}" />
</DataTrigger>
</Style.Triggers>
</Style>
当您不想使用AttachedBehaviors
时就是这种情况。否则我建议在行为中处理故事板清理。这更容易:
void OnDisableTriggersPropertyChanged( object sender, EventArgs args )
{
// If IsDisposed property was changed and it is true now - cleanup triggers.
if ((bool)args.NewValue)
{
var grid = (Grid)sender;
// Ideally you can remove specific triggers.
// Clear all will work for simple cases.
grid.Triggers.Clear();
}
}
总而言之,您必须使用DisableTriggers
依赖项属性添加附加行为,该属性在OnChanged
处理程序中执行清理操作。
如果您只想取消订阅特定活动:
foreach (var eventToUnsubscribe in grid.Triggers.OfType<EventTrigger>()
.Where(x => x.RoutedEvent == UIElement.MouseEnterEvent
|| x.RoutedEvent == UIElement.MouseLeaveEvent).ToList())
{
grid.Triggers.Remove(eventToUnsubscribe);
};