您好,登錄后才能下訂單哦!
在WPF中,為ListView控件項添加動畫效果,可以使用Storyboard和Trigger。以下是一個簡單的示例,展示了如何為ListView控件項的標題添加淡入淡出動畫效果。
首先,在XAML中添加ListView控件:
<Window x:Class="ListViewAnimationExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ListViewAnimationExample"
mc:Ignorable="d"
Title="ListView Animation Example" Height="200" Width="300">
<Grid>
<ListView x:Name="listView" HorizontalAlignment="Left" Height="160" Margin="10,10,0,0" VerticalAlignment="Top" Width="260">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="title" Text="{Binding Title}" FontWeight="Bold" />
<Button Content="Delete" Click="DeleteButton_Click" Margin="5,0,0,0" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
接下來,在MainWindow.xaml.cs中添加數據模型和初始化代碼:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace ListViewAnimationExample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 初始化數據
var items = new[]
{
new Item { Title = "Item 1" },
new Item { Title = "Item 2" },
new Item { Title = "Item 3" },
};
listView.ItemsSource = items;
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button?.CommandParameter is Item item)
{
listView.Items.Remove(item);
}
}
}
public class Item
{
public string Title { get; set; }
}
}
最后,在MainWindow.xaml中添加動畫代碼:
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid>
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentStringFormat="{TemplateBinding ContentStringFormat}" />
</Border>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="title" BeginTime="0:0:0" Duration="0:0:0.5" From="0" To="1" AutoReverse="False" />
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="title" BeginTime="0:0:0" Duration="0:0:0.5" From="0" To="1" AutoReverse="False" />
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="title" BeginTime="0:0:0" Duration="0:0:0.5" From="0" To="1" AutoReverse="False" />
</Storyboard>
</VisualState>
<VisualState x:Name="Inactive">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="title" BeginTime="0:0:0" Duration="0:0:0.5" From="1" To="0" AutoReverse="True" />
</Storyboard>
</VisualState>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
這個示例中,我們為ListViewItem定義了一個模板,并在其中添加了一個名為title的TextBlock。然后,我們為title元素創建了一個淡入淡出動畫,當鼠標懸停在ListView項上時,動畫效果會觸發。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。