在 XAML 中将动画绑定到 ListViewItem

发布于 2024-10-09 17:08:59 字数 1561 浏览 2 评论 0原文

我想为新创建的 ListViewItem 分配一个动画。就像淡入或淡出一样。 我有这个 XAML 代码:

<ListView Height="320" HorizontalAlignment="Left" Margin="630,0,0,0" Name="listView1" VerticalAlignment="Top" Width="222" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Name="mainGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.Resources>

                </Grid.Resources>
                <Image Name="img" Source="{Binding AvatarSource}" Width="32" Margin="8"></Image>
                <Image Source="{Binding IconSource}" Width="16" Margin="0,-28,32,0"></Image>
                <TextBlock Grid.Column="1" Margin="0,8,0,0" TextWrapping="Wrap">
                    <Run Text="{Binding Name}" FontWeight="Bold"></Run>
                    <LineBreak/>
                    <Run Text="{Binding StatusText}"></Run>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

有没有办法像 jQuery 一样以任何方式访问: $('#listViewItemId').fadeIn();

我可以将 listView1.ItemsSource 绑定回某些可访问的表单,在其中我将看到其中的所有控件吗?而不是操纵它们?

我也不想在 C# 中手动执行此操作,但如果使用 ItemTemplate 无法完成此操作,我无论如何都会执行此操作。

我是 XAML 新手,找不到任何好的资源来以正确的方式学习 C# 的 XAML。

I would like to assign an animation to newly created ListViewItem. Like FadeIn or FadeOut.
I have this XAML code:

<ListView Height="320" HorizontalAlignment="Left" Margin="630,0,0,0" Name="listView1" VerticalAlignment="Top" Width="222" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Name="mainGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.Resources>

                </Grid.Resources>
                <Image Name="img" Source="{Binding AvatarSource}" Width="32" Margin="8"></Image>
                <Image Source="{Binding IconSource}" Width="16" Margin="0,-28,32,0"></Image>
                <TextBlock Grid.Column="1" Margin="0,8,0,0" TextWrapping="Wrap">
                    <Run Text="{Binding Name}" FontWeight="Bold"></Run>
                    <LineBreak/>
                    <Run Text="{Binding StatusText}"></Run>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Is there a way to access any way like in jQuery: $('#listViewItemId').fadeIn();

Can I bind back listView1.ItemsSource to some accessible form in which I will see all Controls that are inside it? And than manipulate them?

I also would like to have no to do it manually in C#, but if this is impossible to accomplish using ItemTemplate I will do it anyway.

I am new to XAML and I couldn't find any good source to learn XAML with C# in proper way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-10-16 17:08:59

下面我发布了最简单的代码和 XAML 来实现你想要的。从那里开始,让它在您自己的应用程序中工作。

<Window x:Class="ListviewItemLoadedSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="FadeIn">
            <DoubleAnimation 
                Storyboard.TargetProperty="Opacity" 
                Duration="0:0:2" From="0" To="1"/>
        </Storyboard>
        <Style TargetType="Label" x:Key="FadingLabel">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard Storyboard="{StaticResource FadeIn}"/>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <ListBox ItemsSource="{Binding Data}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Info}"
                           Style="{StaticResource FadingLabel}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
        <Button Name="AddButton"
                Click="AddButton_Click">
            Add Item
        </Button>
    </StackPanel>
</Window>

using System.Collections.ObjectModel;
using System.Windows;

namespace ListviewItemLoadedSpike
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int counter;
        public MainWindow()
        {
            InitializeComponent();
            Data=new ObservableCollection<Datum>();
            DataContext = this;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            counter++;
            Data.Add(new Datum(counter.ToString()));
        }

        public ObservableCollection<Datum> Data { get; set; }
    }

    public class Datum
    {
        public Datum(string info)
        {
            Info = info;
        }

        public string Info { get; set; }
    }
}

祝你好运!

Below I posted the most simple code and XAML that achieves what you want. Go from there to make it work in your own application.

<Window x:Class="ListviewItemLoadedSpike.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="FadeIn">
            <DoubleAnimation 
                Storyboard.TargetProperty="Opacity" 
                Duration="0:0:2" From="0" To="1"/>
        </Storyboard>
        <Style TargetType="Label" x:Key="FadingLabel">
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard Storyboard="{StaticResource FadeIn}"/>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <ListBox ItemsSource="{Binding Data}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Info}"
                           Style="{StaticResource FadingLabel}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
        <Button Name="AddButton"
                Click="AddButton_Click">
            Add Item
        </Button>
    </StackPanel>
</Window>

using System.Collections.ObjectModel;
using System.Windows;

namespace ListviewItemLoadedSpike
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int counter;
        public MainWindow()
        {
            InitializeComponent();
            Data=new ObservableCollection<Datum>();
            DataContext = this;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            counter++;
            Data.Add(new Datum(counter.ToString()));
        }

        public ObservableCollection<Datum> Data { get; set; }
    }

    public class Datum
    {
        public Datum(string info)
        {
            Info = info;
        }

        public string Info { get; set; }
    }
}

Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文