Apply animation inside DataTemplate - windows-phone-7

I have a listbox with the following structure. Data displays well and no issues there. I want to get some animation affect for the StackPanel, but since it is inside the DataTemplate, I am not able to use Blend to get my desired animation affect.
I guess I can write an event handler and code for that, but is that the best approach of achieving animation for ListBox items?
<ListBox Height="600" ItemsSource="{StaticResource learn}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="100" Orientation="Horizontal">
<TextBlock Width="0" Text="{Binding ID}" />
<Image Height="100" Width="100"/>
<StackPanel Orientation="Vertical" Width="319" VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="{Binding Text}" Margin="6,0,0,0" FontSize="29.333" />
<TextBlock TextWrapping="Wrap" Text="{Binding Description}" Margin="6,0,0,0" FontSize="16"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Okay, all you need to do is to create a new user control and add all your animations to that user control and start animation where you want. In the list box data template, create an instance of that data template.
Steps to reproduce:
Create a new User Control in the project.
Give in the animations to the animations to that user control in blend.
Add the user control in data template.
Start the animations where ever you want.
Alternative: Without creating a new user control you can specify animations by creating storyboard and applying required transformations. this gives a common animation to all the list box items.

Related

WP7: How to effectively load 100+ small pictures in one view

I am trying to make a long ListBox with each ListBoxItem being a tile with a small 100x100 logo inside it. Right now it's very slow - using more than 5-6 seconds just to become responsive. Since the images are downloaded from the web async (and each one is stored inside each own model instance), I don't know how to check when they have all downloaded either (i.e. no progress indicator to cover the delay).
Do you have any idea as to how I can do this in the most effective way possible?
XAML:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Template>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<ListBoxItem>
<Grid Name="ChannelTile" Tap="ChannelTile_Tap">
<Rectangle Fill="{StaticResource LightGrayColor}" />
<Image Style="{StaticResource Tiles_Image}" Source="{Binding Image}" />
</Grid>
</ListBoxItem>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If you reference the images in binding (Source={Binding Image}) by URL, try using the LowProfileImageLoader from PhonePerformance (http://nuget.org/packages/PhonePerformance)

How to make a custom listview with one image and one textbox in windows7 phone?

i need to make a custom listview in windows7 phone. In this listview, i need to show one image with one textbox. This listview will be dynamic. i will insert value in database and this listview will be generate till the last value of my database.
I am new in windows7 phone, any suggestion will be appricated.
You need to set the ItemTemplate:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=ImageSource}"/>
<TextBlock Text="{Binding Path=Text"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox>
The above code assumes you have bound a list, or set the ItemsSource, to a list of objects that expose Text and ImageSource properties.

Items collection must be empty before using error

I using the toolkit which provide the MultiSelectionList control in wp7, I am trying to bind the names to the multiselectItem using the property ItemsSource in C#.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBlock Text="Please select the satellites from the list:-" />
<toolkit:MultiselectList Name="multiSelectionList">
<toolkit:MultiselectItem Content="{Binding Name}" />
</toolkit:MultiselectList>
</StackPanel>
</Grid>
But I got this error.
Items collection must be empty before using ItemsSource.
I tested the service in other parts of the app and it is working with no issues.
Please advise me.Thanks,
You're adding an item to the MultiSelectList when you do:
<toolkit:MultiselectItem Content="{Binding Name}" />
By the time you assign the ItemsSource, which I assume you're doing in the code-behind somewhere, there is already an item in the list (the one above). This is why the error is getting thrown. You could manually clear the list before setting the source, but that's not necessarily considered good practice. What you could do instead is create a DataTemplate, not an actual instance of a MultiSelectItem. I'm not familiar with this control, but try:
<toolkit:MultiselectList Name="multiSelectionList">
<toolkit:MultiSelectList.ItemTemplate>
<DataTemplate>
<toolkit:MultiselectItem Content="{Binding Name}" />
</DataTemplate>
</toolkit:MultiSelectList.ItemTemplate>
</toolkit:MultiselectList>

ListboxItems containing image - memory issues

I have a design issue with my listbox on my view.
Currently it has following DataTemplate as ItemTemplate:
<DataTemplate x:Key="MovieItemTemplate">
<StackPanel>
<Border BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="5" Margin="3" Height="215" Width="140">
<Image x:Name="MovieCover"
toolkit:TiltEffect.IsTiltEnabled="True"
Margin="0"
HorizontalAlignment="Center"
Width="140"
Height="210">
<Image.Source>
<BitmapImage UriSource="{Binding Cover}" CreateOptions="BackgroundCreation"/>
</Image.Source>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<gsextra:EventToCommand Command="{Binding MainViewModel.MovieItemSelectedCommand, Source={StaticResource Locator}}"
PassEventArgsToCommand="True"
CommandParameter="{Binding MovieID}"
></gsextra:EventToCommand>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</Image>
</Border>
</StackPanel>
</DataTemplate>
So as you can see, I have an image inside it that will download his content from the internet through an URI. Thanks to the new mango option BackgroundCreation ( cfr. http://blogs.msdn.com/b/slmperf/archive/2011/06/13/off-thread-decoding-of-images-on-mango-how-it-impacts-you-application.aspx ) it load in background.
But I have a very large collection and even though I do 'paged' binding of the Listbox Source, I notice my memory keeps on going up, until it has consumed everything and the app crashes.
Then, I noticed this http://blog.wpfwonderland.com/2011/01/17/images-and-memory-leaks-in-windows-phone-7/ so it would seem I need to cleanup the image itself, because of the image caching feature in wp7.
When I do this, everything works great in reference to memory, BUT now each time the user 'pages' through the listbox the images need to be redownloaded, resulting in an app that is almost not usable... because the user keeps on waiting for those images.
Any tips/tricks on how to go about this?
I also tried the DefferedLoadListBox

Windows Phone 7 Linking to events inside DataTemplates

I have a listbox where I create a Itemtemplate with a DataTemplate. I want to be able to write events for the checkboxes and buttons in the datatemplate but they do not seem to be firing.
Here is my xaml and basically I just tried to display a messagebox.show("worked") in the event function.
<ListBox x:Name="ListBox_Items" Margin="0,91,0,8" Foreground="#FF4BE5DB">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="700">
<CheckBox IsChecked="{Binding needPurchase}" Click="NeedPurchase_Click" Name="CheckBox_NeedPurchase"/>
<CheckBox IsChecked="False" Name="InCart"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding storeLocation}"/>
</StackPanel>
<Button HorizontalAlignment="Right" Content="DELETE" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Because the items are defined within a DataTemplate they are not hooked up to the code-behind for the parent class. If you want to handle events for templated items, then you should consider using commands instead. If you don't know what commands are (and therefore unlikely to know what MVVM is), then you should check out an explanation like this by Jeremy Likness.
I agree that using commands is the best approach.
However if you still want to assess controls placed inside the ItemTemplate/DataTemplate (and subscribe to some events), then you can do this by using the VisualTreeHelper.
For starters you need to remove the name from all the controls in the template. If you have 10 items in the list you will have 10 sets of controls with the same name which won't work.

Resources