ListBoxItem 动画和不透明度
我有一个带有样式的列表框,包括带有样式的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 画笔。
问题在于不透明动画,最好在底部图片中看到:
这是动画中间的屏幕截图(当时列表框项目的不透明度为 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是系统相关的渲染异常,我尝试了这种样式(只需使用我自己选择的项目图像,也是透明的 PNG)并且效果很好。
在我的一个应用程序中,设置为
Rectangle
的Fill
属性的图像在我的一台计算机(甚至具有相同的操作系统 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 ofRectangle
s were weirdly stretched on one of my computers (which even had the same operating system, Win7 Professional) so that would not be unheard of...我已经解决了这个问题。问题是在代码中我覆盖了窗口的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.