Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Binding to an anonymous type with a data template causes lockup of browser
3 replies. Latest Post by Allen Chen – MSFT on March 20, 2008.
(0)
bpatters
Member
50 points
24 Posts
03-19-2008 1:37 AM |
I have a simple listbox with a data template like such:
<
<ListBox
When I've downloaded the item data I use the following code to set the data context.
}).ToList();
lbComments.DataContext =data;
If I use the data template then the browser locks up. If I don't then it shows the elements correctly (calls tostring to view it).
mchlsync
Star
14606 points
2,730 Posts
03-19-2008 2:03 AM |
I think this might be an alternative issue of this issue that I reported earlier. Please take a look this link. http://silverlight.net/forums/p/11147/36232.aspx
Edit: I posted the wrong link earlier and now, I updated the link. thanks.
03-19-2008 8:57 AM |
It doesn't look related to me . I'm using a listbox and it shows data fine except when I use a DataTemplate with it.
Allen Ch...
13854 points
1,800 Posts
03-20-2008 10:20 PM |
Hi:
I think it's the same known issue. I've found a way to work it around using reflection:
using System;using System.Collections.Generic;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Reflection;namespace SilverlightApplication3{ public partial class Page : UserControl { List<Customer> list = new List<Customer>(); public Page() { InitializeComponent(); list.Add(new Customer() { Name = "Name1" }); list.Add(new Customer() { Name = "Name2" }); var q = (from c in list select new { theName = c.Name + "_Hello"}).ToList(); this.ListBox1.ItemsSource = q; }
private void StackPanel_Loaded(object sender, RoutedEventArgs e) { StackPanel sp = sender as StackPanel; PropertyInfo[] propertys = ((Grid)(sp.Parent)).DataContext.GetType().GetProperties(); foreach (PropertyInfo p in propertys) { if (p.PropertyType == typeof(string)&&p.Name=="theName") { TextBlock tb = sp.FindName("subTextBlock1") as TextBlock; if (tb != null) { tb.Text = p.GetValue(((Grid)(sp.Parent)).DataContext, null).ToString(); } } }
} } public class Customer { public string Name { get; set; }
}
<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication3.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <UserControl.Resources> <DataTemplate x:Name="CommentDataTemplate">
<StackPanel Loaded="StackPanel_Loaded" Orientation="Vertical"><TextBlock x:Name="subTextBlock1"></TextBlock> </StackPanel>
</DataTemplate>
</UserControl.Resources> <Grid x:Name="LayoutRoot" Background="Black"> <StackPanel> <ListBox x:Name="ListBox1" ItemTemplate="{StaticResource CommentDataTemplate}"> </ListBox> </StackPanel> </Grid></UserControl>
Regards