Skip to main content

Microsoft Silverlight

Answered Question Binding to an anonymous type with a data template causes lockup of browserRSS Feed

(0)

bpatters
bpatters

Member

Member

50 points

24 Posts

Binding to an anonymous type with a data template causes lockup of browser

I have a simple listbox with a data template like such:

<DataTemplate x:Name="CommentDataTemplate">

<StackPanel Orientation="Vertical">

<TextBlock Text="{Binding name}"/>

<Border Background="LightGray" BorderBrush="Black" BorderThickness="2">

<TextBox Height="100" IsReadOnly="True" Text="{Binding message}"/>

</Border>

</StackPanel>

</DataTemplate>

<ListBox Grid.Row="0" x:Name="lbComments" ItemTemplate="{StaticResource CommentDataTemplate}" ItemsSource="{Binding}"/>

 

When I've downloaded the item data I use the following code to set the data context.

var
data = (from com in result.Content.Descendants("comment")

select new {

name = com.Element("name").Value,message = com.Element("message").Value

}).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
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Binding to an anonymous type with a data template causes lockup of browser

 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.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


bpatters
bpatters

Member

Member

50 points

24 Posts

Re: Binding to an anonymous type with a data template causes lockup of browser

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 Chen – MSFT
Allen Ch...

Star

Star

13854 points

1,800 Posts

Answered Question

Re: Binding to an anonymous type with a data template causes lockup of browser

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

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities