ListBoxItem 动画和不透明度

发布于 2024-10-11 16:13:55 字数 3958 浏览 2 评论 0原文

我有一个带有样式的列表框,包括带有样式的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 画笔。

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

Screenshot

这是动画中间的屏幕截图(当时列表框项目的不透明度为 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>

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

感谢您的帮助。

I have a Listbox with styles, including Listboxitem with styles. I am trying to create an animation that changes opacity from 0 to 1, to make items show on the list. I've managed to do this with the following code:

<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>

The thing works as it should (apart from that I want more time to pass between current and next item animation start. But it has a problem with opacity. Everything possible is set to transparent, backgrounds and all. And I use transparent .png brush for selected item.

The problem is with the opacity animation and is best seen on the bottom picture:

Screenshot

This is a screenshot in the middle of animation (at the time the opacity of the listboxitems is 0.8) and you can clearly see white background around all text. It's even more visible in the first selected item, because it uses transparent .png. This background magically dissapears when animation is finished and the opacity is 1.0.

How to fix this problem? Did I forget to set any background perhaps?

Thank you for your help!

Edit:

I am adding my listbox declaration:

<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>

Also another question is: How to delay the animation that each listboxitem would be displayed with a delay of few milliseconds before the next one?

Thank you for all your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2024-10-18 16:13:55

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

This might be a system dependent rendering anomaly, i tried that style (only had to use my own selected item image, also a transparent PNG) and it worked quite nicely.
In one application of mine images set to the Fill property of Rectangles were weirdly stretched on one of my computers (which even had the same operating system, Win7 Professional) so that would not be unheard of...

锦上情书 2024-10-18 16:13:55

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

I have fixed the problem. The problem was that in code I was overriding window's AllowTransparency flag. Now it works as it should. If anyone would encounter a similar problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文