【问题标题】:ListBoxItem animation and opacityListBoxItem 动画和不透明度
【发布时间】:2012-05-31 04:27:48
【问题描述】:

我有一个带有样式的 Listbox,包括带有样式的 Listboxitem。我正在尝试创建一个将不透明度从 0 更改为 1 的动画,以使项目显示在列表中。我已经设法使用以下代码做到了这一点:

<Style x:Key="ListBoxStyle1" TargetType="ListBox">
            <Setter Property="Foreground" Value="#FF393C3F" />
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <Border Name="Border" Background="{x:Null}" BorderBrush="Black" BorderThickness="0" Padding="0">
                            <ItemsPresenter />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
            <Setter Property="Opacity" Value="0" />
            <Setter Property="Height" Value="16" />
            <Setter Property="VerticalContentAlignment" Value="Bottom" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="Border" Padding="10,1,0,0" Background="{x:Null}">
                            <ContentPresenter VerticalAlignment="Center" SnapsToDevicePixels="True" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource arrow}" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Foreground" Value="#FF828689" />
                            </Trigger>
                            <Trigger Property="IsVisible" Value="true">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" Duration="0:0:0.4" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

事情按原样工作(除此之外,我希望在当前和下一个项目动画开始之间有更多时间。但它有不透明度问题。一切可能都设置为透明,背景和所有。我使用透明所选项目的 .png 画笔。

问题在于不透明动画,最好在底部图片上看到:

这是动画中间的截图(当时listboxitems的不透明度为0.8),你可以清楚地看到所有文本周围的白色背景。它在第一个选定的项目中更加明显,因为它使用透明的 .png。当动画完成并且不透明度为 1.0 时,此背景会神奇地消失。

如何解决这个问题?是不是我忘记设置背景了?

感谢您的帮助!

编辑:

我正在添加我的列表框声明:

<ListBox Height="239" HorizontalAlignment="Left" Margin="0,0,0,0" Name="listBox1" VerticalAlignment="Top" Width="145" Background="{x:Null}"  FontWeight="Black" FontSize="8" BorderBrush="{x:Null}" SnapsToDevicePixels="True" BorderThickness="0" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" Style="{StaticResource ListBoxStyle1}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel VerticalAlignment="Top" Background="{x:Null}" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

另外一个问题是:如何延迟每个列表框项在下一个之前几毫秒的延迟显示的动画?

感谢您的所有帮助。

【问题讨论】:

  • 你能添加你的列表框声明吗?我已将此样式插入到我的一个程序中,并且没有遇到您遇到的问题。
  • 谢谢你的回答,斯科特。我已经添加了我的列表框声明。我做了更多的测试,它绝对不依赖于系统。问题出在我的应用程序或 WPF 中。这个问题甚至不需要动画。第二个列表框项目的不透明度设置为不同于 0 或 1.0 的样式,它以白色背景呈现。即使是没有背景图片的列表框项目。我帖子中的图片以列表框的 0.8 不透明度呈现。

标签: c# wpf xaml animation wpf-controls


【解决方案1】:

这可能是一个依赖于系统的渲染异常,我尝试了这种风格(只需要使用我自己选择的项目图像,也是透明的 PNG)并且效果很好。
在我的一个应用程序中,设置为Rectangles 的Fill 属性的我的图像在我的一台计算机(甚至具有相同的操作系统,Win7 Professional)上被奇怪地拉伸,所以这不会是闻所未闻的......

【讨论】:

  • 感谢您的回答,但我不认为您是对的,因为它在多台计算机上呈现相同。我认为这是一个 WPF 问题,导致它的原因可能是另一种样式/模板的一部分,而不是这个。一旦我的不透明度不同于 0 或 1,我就会得到带有项目的白色背景。就像我发布的图片上的样子。尝试在您的应用程序中填充背景,让我知道它是否有效。问候。
  • 尝试将背景放入白色阴影中,例如#DDDDDD(我使用的是#FAF6F9)。
【解决方案2】:

我已经解决了这个问题。问题是在代码中我覆盖了窗口的 AllowTransparency 标志。现在它可以正常工作了。如果有人会遇到类似的问题。

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2010-09-25
    • 2012-09-09
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2018-05-27
    • 2013-08-01
    相关资源
    最近更新 更多