ItemsPanelTemplate Differences in SL and WPF
I was changing the ItemsPanelTemplate of Listbox and set the Position of Elements generated using TranslateTransform
WPF app works fine, while the Silverlight app crashes. As far as I can tell the code is same
Here is the XAML
<UserControl x:Class=”t14.Page”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation“
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml“
Width=”400″ Height=”300″>
<UserControl.Resources>
<DataTemplate x:Key=”dt”>
<TextBlock Text=”{Binding Name}”>
<TextBlock.RenderTransform>
<TranslateTransform X=”{Binding Position.X}”
Y=”{Binding Position.Y}” />
</TextBlock.RenderTransform>
</TextBlock>
</DataTemplate>
</UserControl.Resources>
<Canvas x:Name=”c1″>
<ListBox x:Name=”list1″ ItemTemplate=”{StaticResource dt}” >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width=”500″ Background=”Azure”
Height=”500″></Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Canvas>
</UserControl>
and code behind
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Name = “Customer..1″, Position = new Point {X=100,Y=100 } });
customers.Add(new Customer { Name = “Customer..2″, Position = new Point { X = 50, Y = 50 } });
customers.Add(new Customer { Name = “Customer..3″, Position = new Point { X = 150, Y = 150 } });
customers.Add(new Customer { Name = “Customer..4″, Position = new Point { X = 200, Y = 200 } });
list1.ItemsSource = customers;
}
}
public class Customer
{
public Point Position { get; set; }
public string Name { get; set; }
}
I also tried to set ItemContainerStyle like this in SL, but it crashes
<Style x:Key=”st1″
TargetType=”ListBoxItem”>
<Setter Property=”Canvas.Left”
Value=”{Binding Position.X}” />
<Setter Property=”Canvas.Top”
Value=”{Binding Position.X}” />
</Style>
I set same thing in WPF, it works
<Style x:Key=”st1″ TargetType=”{x:Type ListBoxItem}”>
<Setter Property=”Canvas.Left”
Value=”{Binding Position.X}” />
<Setter Property=”Canvas.Top”
Value=”{Binding Position.X}” />
</Style>
you can download the sample app here


