Windows Phone 7 Linking to events inside DataTemplates - windows-phone-7

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.

Related

Apply animation inside DataTemplate

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.

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>

ListPicker crashes on full screen - Silverlight tool kit for WP7

When implementing a ListPicker, it will crash when there are enough items to make it full screen. It does not crash if there are only 2-3 items and it just expands. I get an ArgumentException, 'The parameter is incorrect'
<toolkit:ListPicker Grid.Row="1"
ItemTemplate="{Binding lpkItemTemplate}"
FullModeItemTemplate="{Binding lpkFullItemTemplate}">
<toolkit:ListPicker.Items>
<toolkit:ListPickerItem>1</toolkit:ListPickerItem>
<toolkit:ListPickerItem>5</toolkit:ListPickerItem>
<toolkit:ListPickerItem>10</toolkit:ListPickerItem>
<toolkit:ListPickerItem>15</toolkit:ListPickerItem>
<toolkit:ListPickerItem>20</toolkit:ListPickerItem>
<toolkit:ListPickerItem>30</toolkit:ListPickerItem>
</toolkit:ListPicker.Items>
</toolkit:ListPicker>
Templates are
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="lpkItemTemplate">
<TextBlock Text="{Binding Content}" />
</DataTemplate>
<DataTemplate x:Name="lpkFullItemTemplate">
<TextBlock Text="{Binding Content}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
I've looked at examples and I havent seen anything different than what I have here and it works. The only difference is the examplesa re doing data binding. I've tried steping through the control's code but I don't see anything that pops. It throws the exception in the base class 'ItemsControl' after the 'OnManipulationCompleted' event handler has completed in the ListPicker.
Any ideas what i'm doing wrong?
FullMode only works with DataBinding, and not with static ListPickerItems.

VS2010 - Looking for Designer Support Workaround for ListBox DataTemplate Issue

I have a small problem with the VS2010 Designer in a WP7 project. The XAML looks like this:
<ListBox ItemsSource="{Binding Children}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Tools:ButtonBaseExtensions.Command="{Binding ClickedCommand}"
Style="{StaticResource InvisibleButtonStyle}">
<Grid DataContext="{Binding Gui}">
<TextBlock Text="{Binding Label}"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Which works as expected in the emulator, but does not give me any output in the designer.
If I change the code to this, however, the designer works fine
<ListBox ItemsSource="{Binding Children}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Tools:ButtonBaseExtensions.Command="{Binding ClickedCommand}"
Style="{StaticResource InvisibleButtonStyle}">
<Grid>
<TextBlock Text="{Binding Gui.Label}"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Anyone can confirm that this is a designer bug? And if so, any simple workaround? I do not want to change my XAML or structure my controls differently, only to support the designer.
Thanks,
Chris
There have been a few people asking similar questions—relating to nesting item collections within bindings. The general experience is that if you have items within items then things may not behave as you expect (or may see elsewhere).
That it behaves differently is probably not intended.
Try the "official" forums (http://forums.create.msdn.com/forums/98.aspx) for confirmation as to whether this is a "bug" or not.
You may also want to reconsider your model structure in the mean time as you may hit other related issues. Lists in lists can also have performance issues and are recommended too be avoided if possible.

Resources