溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

ListView控件項(xiàng)動(dòng)畫(huà)效果添加

發(fā)布時(shí)間:2024-09-10 17:11:41 來(lái)源:億速云 閱讀:79 作者:小樊 欄目:編程語(yǔ)言

在WPF中,為L(zhǎng)istView控件項(xiàng)添加動(dòng)畫(huà)效果,可以使用Storyboard和Trigger。以下是一個(gè)簡(jiǎn)單的示例,展示了如何為L(zhǎng)istView控件項(xiàng)的標(biāo)題添加淡入淡出動(dòng)畫(huà)效果。

首先,在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>

接下來(lái),在MainWindow.xaml.cs中添加數(shù)據(jù)模型和初始化代碼:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace ListViewAnimationExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // 初始化數(shù)據(jù)
            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中添加動(dòng)畫(huà)代碼:

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

這個(gè)示例中,我們?yōu)長(zhǎng)istViewItem定義了一個(gè)模板,并在其中添加了一個(gè)名為title的TextBlock。然后,我們?yōu)閠itle元素創(chuàng)建了一個(gè)淡入淡出動(dòng)畫(huà),當(dāng)鼠標(biāo)懸停在ListView項(xiàng)上時(shí),動(dòng)畫(huà)效果會(huì)觸發(fā)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI