Layout Cycle error when using many TextClipper controls...

2 posts, 0 answers
  1. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 06 Jul 2009 Link to this post

    I am getting a "Layout Cycle Detected" error when using many TextClipper controls in my application.  I have managed to isolate the issue in a small silverlight project (see below).  You can see the problem by adding around 250 items to the ItemsControl and resizing the window.

    1 <UserControl x:Class="SilverlightApplication25.MainControl" 
    2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    4 xmlns:TelerikControls_Primitives="clr-namespace:Telerik.Windows.Controls.Primitives;assembly=Telerik.Windows.Controls" 
    5 > 
    6   <Grid> 
    7  
    8     <Grid.RowDefinitions> 
    9       <RowDefinition Height="50" /> 
    10       <RowDefinition Height="1*" /> 
    11     </Grid.RowDefinitions> 
    12      
    13     <Button x:Name="btn" Content="0 items" Click="Button_Click" Grid.Row="0" /> 
    14  
    15     <ScrollViewer Grid.Row="1">  
    16       
    17       <ItemsControl x:Name="itemscontrol" ItemsSource="{Binding}">  
    18         <ItemsControl.ItemTemplate> 
    19           <DataTemplate> 
    20             <TelerikControls_Primitives:TextClipper Content="{Binding Path=MyProperty}" Foreground="#FF002D62" FontFamily="Portable User Interface" FontWeight="Normal" FontSize="20" IsTabStop="False" ToolTipService.ToolTip="{Binding Path=MyProperty}" /> 
    21           </DataTemplate> 
    22         </ItemsControl.ItemTemplate> 
    23       </ItemsControl> 
    24     </ScrollViewer> 
    25   </Grid> 
    26 </UserControl> 
    1 using System.Windows;  
    2 using System.Windows.Controls;  
    3  
    4 namespace SilverlightApplication25  
    5 {  
    6   public partial class MainControl : UserControl  
    7   {  
    8     public System.Collections.ObjectModel.ObservableCollection<MyObject> TheList = new System.Collections.ObjectModel.ObservableCollection<MyObject>();  
    9           
    10     public MainControl()  
    11     {  
    12       InitializeComponent();  
    13       this.itemscontrol.DataContext = TheList;  
    14     }  
    15  
    16     private void Button_Click(object sender, RoutedEventArgs e)  
    17     {  
    18       for (int i = 1; i <= 50; i++)  
    19         TheList.Add(new MyObject() { MyProperty = System.Guid.NewGuid().ToString() });  
    20       this.btn.Content = string.Format("{0} items", TheList.Count.ToString());  
    21     }  
    22           
    23     public class MyObject  
    24     {  
    25       public string MyProperty { getset; }  
    26     }  
    27   }  
    28
  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 07 Jul 2009 Link to this post

    Hello Rob,

    Thanks for bringing our atention to the issue.

    Currently in Silverlight there is no auto-clipping feature (as in WPF) and we created this control that tries to do just this. Unfortunately every TextClipper control invalidates the layout at least once (i.e. is measured twice) because there is no way of knowing the size of the text before it has been rendered.

    I suppose that this can be implemented naively, assuming an average width for the font characters depending on their size. This may save a few layout cycles and prevent the error. This of course would mean that the clipping will not be very accurate because in most fonts there is width difference between characters, say 'i' and 'W'.

    The reason why you get the error around 250 elements is because the you cannot invalidate the layout more than 256 times in a single pass.

    If you are interested in the "naive" TextClipper, please come back to us and I will prepare an example with sample implementation.

    Thanks again for your feedback,

    Best wishes,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top