Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Animation to ListViewItem in XAML

Tags:

c#

wpf

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.

like image 243
Daniel G Avatar asked Jan 02 '11 18:01

Daniel G


1 Answers

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!

like image 52
Dabblernl Avatar answered Nov 06 '22 02:11

Dabblernl