<TabControl>
<TabItem Header="item 1">
<ListBox>
<ListBoxItem>sub item 1</ListBoxItem>
<ListBoxItem>sub item 2</ListBoxItem>
<ListBoxItem>sub item 3</ListBoxItem>
</ListBox>
</TabItem>
<TabItem Header="item 2">
<ListBox>
<ListBoxItem>sub item 1</ListBoxItem>
<ListBoxItem>sub item 2</ListBoxItem>
</ListBox>
</TabItem>
</TabControl>
<Style TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"
VisibleChanged="onStackPanelVisibilityChange"
Loaded="onStackPanelLoaded"
VerticalAlignment="Center" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
你会注意到我在堆栈面板上有一些事件处理程序。这些是为堆栈面板中的项目设置动画,以便它们按顺序淡入视图。事件处理程序实现为:
void onStackPanelLoaded(object sender, RoutedEventArgs e)
{
StackPanel panel = sender as StackPanel;
applySubItemAnimations(panel);
}
void onStackPanelVisibilityChange(object sender, DependencyPropertyChangedEventArgs e)
{
StackPanel panel = sender as StackPanel;
if (panel.IsVisible)
{
applySubItemAnimations(panel);
}
}
private void applySubItemAnimations(StackPanel panel)
{
DoubleAnimation fadeIn = new DoubleAnimation();
fadeIn.DecelerationRatio = 0.1;
fadeIn.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500));
fadeIn.From = 0.0;
fadeIn.To = 1.0;
for (int i = 0; i < panel.Children.Count; i++)
{
panel.Children[i].Opacity = 0.0;
fadeIn.BeginTime = new TimeSpan(0, 0, 0, 0, 200 * i + 50);
panel.Children[i].BeginAnimation(UIElement.OpacityProperty, fadeIn);
}
}
在大多数情况下,这非常有效。第一次单击(或加载)选项卡时,“堆栈”面板中的子项会依次淡入视图。问题是,当您单击返回到之前已经显示过一次的选项卡时(即,您在“VisibleChanged”事件处理程序中,而不是在“Loaded”处理程序中),所有项目都已显示,并且它们
眨眼
为了有序,而不是一开始就被隐藏起来
整齐
panel.Children[i].Opacity = 0.0;
... 什么也不做。如果我在调试器中单步执行代码,并在“panel.Children[I].Opacity”上放置一个手表,它将保持在1.0。没有例外。它只是。。。不起作用。