【发布时间】: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