【问题标题】:Why can't I set the Opacity in this situation?为什么我不能在这种情况下设置不透明度?
【发布时间】:2010-10-16 11:02:16
【问题描述】:

情况是这样的……在顶层,我有一个 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>

ListBox 有一个水平方向的 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>

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

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。没有例外或任何东西。它只是......不起作用。

有什么想法吗?

【问题讨论】:

  • 查看输出窗口,wpf在很多情况下不会抛出异常而是写入输出窗口。
  • 那里没有弹出任何内容,但有一个很好的提示(尤其是对于像我这样的 WPF n00b)。

标签: c# wpf listbox tabcontrol opacity


【解决方案1】:

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

来删除这些动画
panel.Children[i].BeginAnimation(UIElement.OpacityProperty, null);

之后,您可以应用新动画,因此整个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);        
}

【讨论】:

  • 遇到了与此完全相同的问题,而且很有趣,听起来 dkeen 正在编写与我几乎完全相同类型的控件!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 2020-07-27
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
相关资源
最近更新 更多