我需要一些建议。有谁知道如何在wpf中制作这个ListView动画。我需要ListView项目动画对称地运行,见gif 1.
但问题是ListView不能滚动,项目通常会被替换,因为VirtualizingStackPanel的设置是HorizontalAlignment=“Center”,而ListView的设置是ScrollViewer.CanContentScroll=“False”。
那么,我如何修改代码,使ListView中的项的动画是对称的,同时保持滚动。
我现在的代码看起来像这样:
<ListView
x:Name="ListView1"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ScrollViewer.CanContentScroll="False"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel
HorizontalAlignment="Center"
CacheLength="10,10"
CacheLengthUnit="Pixel"
IsItemsHost="True"
IsVirtualizing="True"
Orientation="Horizontal"
ScrollUnit="Pixel"
VirtualizationMode="Recycling">
</VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListViewItem>
<Button
Width="100"
Height="120"
Margin="3"
HorizontalAlignment="Center"
FontSize="18"
FontWeight="Bold"
Foreground="MediumPurple"
MouseEnter="Button_MouseEnter"
MouseLeave="Button_MouseLeave" />
</ListViewItem>
<!-- more items -->
</ListView>
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
var button = sender as Button;
var WidthAnimation = new DoubleAnimation() { To = 130, Duration = TimeSpan.FromSeconds(0.3) };
var HeightAnimation = new DoubleAnimation() { To = 150, Duration = TimeSpan.FromSeconds(0.3) };
button.BeginAnimation(Button.WidthProperty, WidthAnimation);
button.BeginAnimation(Button.HeightProperty, HeightAnimation);
}
private void Button_MouseLeave(object sender, MouseEventArgs e)
{
var button = sender as Button;
var WidthAnimation = new DoubleAnimation() { To = 100, Duration = TimeSpan.FromSeconds(0.3) };
var HeightAnimation = new DoubleAnimation() { To = 120, Duration = TimeSpan.FromSeconds(0.3) };
button.BeginAnimation(Button.WidthProperty, WidthAnimation);
button.BeginAnimation(Button.HeightProperty, HeightAnimation);
}
1条答案
按热度按时间cwxwcias1#
向ItemsPanel添加额外的水平边距,并在按钮放大时减小水平边距,以补偿增加的宽度。
为简洁起见,省略了高度动画。