为什么我不能在这种情况下设置不透明度?

dke*_*een 2 c# wpf tabcontrol listbox opacity

这是情况......在顶层,我有一个TabControl.TabControl中的每个页面都包含一个ListBox:

<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>
Run Code Online (Sandbox Code Playgroud)

ListBoxes有一个水平方向的StackPanel作为它们的ListTemplate:

<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>
Run Code Online (Sandbox Code Playgroud)

您会注意到我在堆栈面板上有一些事件处理程序.这些是为堆栈面板中的项目设置动画,因此它们会依次淡入视图.事件处理程序实现为:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,这很有效.首次单击(或加载)选项卡时,堆栈面板中的子项将逐个淡入视图.问题是,当您单击回到之前已经显示过一次的选项卡(即,您处于"VisibleChanged"事件处理程序而不是"已加载"处理程序)时,所有项目都已显示并且它们按顺序闪烁,而不是开始为隐藏和被显示在顺序.

这是丑陋的地方.这一行:

panel.Children[i].Opacity = 0.0;
Run Code Online (Sandbox Code Playgroud)

... 什么也没做.如果我单步执行调试器中的代码并将注意放在"panel.Children [i] .Opacity"上,它将保持在1.0.没有例外或任何事情.它只是...不起作用.

有任何想法吗?

EFr*_*ank 7

我猜测可能发生的事情:WPF在完成后不会移除动画,因此当您的代码applySubItemsAnimations第二次运行该方法时,之前的动画仍然存在.因此,您可以尝试通过将null作为第二个参数传递给删除这些动画

panel.Children[i].BeginAnimation(UIElement.OpacityProperty, null);
Run Code Online (Sandbox Code Playgroud)

之后,您可以应用新动画,因此整个for-loop看起来像这样:

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, null);        
    panel.Children[i].BeginAnimation(UIElement.OpacityProperty, fadeIn);        
}
Run Code Online (Sandbox Code Playgroud)