This is a migrated thread and some comments may be shown as answers.

application not working after update to q3

10 Answers 107 Views
Carousel
This is a migrated thread and some comments may be shown as answers.
Pedro
Top achievements
Rank 1
Pedro asked on 02 Mar 2010, 11:29 PM
Hi,
I have a simple app very similar to the carousel demo (First look), the app was working fine with 2009 Q2, but  after upgrade to 2009 Q3, the app throws the following error:

System.Reflection.TargetInvocationException was unhandled
  Message="Exception has been thrown by the target of an invocation."
  Source="mscorlib"
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Application.RunInternal(Window window)
       at CuiraWPF.App.Main() in D:\projects2\own\XMPP messenger\client\Cuira\CuiraWPF\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       Message="Added item does not appear at given index '3'."
       Source="PresentationFramework"
       StackTrace:
            at System.Windows.Data.ListCollectionView.AdjustBefore(NotifyCollectionChangedAction action, Object item, Int32 index)
            at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
            at Telerik.Windows.Data.DataItemCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\DataItemCollection.cs:line 506
            at Telerik.Windows.Data.DataItemCollection.Telerik.Windows.Data.IWeakEventListener<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.ReceiveWeakEvent(Object sender, NotifyCollectionChangedEventArgs e) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\DataItemCollection.cs:line 725
            at Telerik.Windows.Data.WeakEvent.WeakListener`1.Handler(Object sender, TArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\WeakEvents\WeakEvent.cs:line 32
            at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
            at Telerik.Windows.Data.QueryableCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\QueryableCollectionView.cs:line 482
            at Telerik.Windows.Data.QueryableCollectionView.RefreshAndRaiseCollectionChanged(NotifyCollectionChangedEventArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\QueryableCollectionView.cs:line 557
            at Telerik.Windows.Data.QueryableCollectionView.OnSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\QueryableCollectionView.cs:line 640
            at Telerik.Windows.Data.QueryableCollectionView.Telerik.Windows.Data.IWeakEventListener<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.ReceiveWeakEvent(Object sender, NotifyCollectionChangedEventArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\Collections\QueryableCollectionView.cs:line 631
            at Telerik.Windows.Data.WeakEvent.WeakListener`1.Handler(Object sender, TArgs args) in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\WeakEvents\WeakEvent.cs:line 32
            at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
            at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
            at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)
            at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
            at System.Collections.ObjectModel.Collection`1.Add(T item)
            at CuiraWPF.Code.MessengerHelper.<>c__DisplayClass17.<Connection_OnRosterItem>b__13() in D:\projects2\own\XMPP messenger\client\Cuira\CuiraWPF\Code\MessengerHelper.cs:line 305
       InnerException: 

The code line 305 is just a : ContactList.Add(current);

Any clues?

Additional information:
I found a temp fix:
I have an event handler that is listening when the collection changes, every time the observable collection is changed, it is reordered, I created a class that inherits from ObservableCollection, I have used that class on Silverlight with your components and it works fine, as I mentioned, the code works fine with Q2 and WPF, my temp solution was to comment the sort code and its working now, not the way that I need, but at least is displaying data.

10 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 08 Mar 2010, 08:56 AM
Hello Pedro,

We couldn't reproduce the problem. May I ask you to send us a sample project that will allow us to reproduce the exception.

Thank you for your time.


Regards,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:52 PM
Sure, how can I send to you the sample project since I cannot open a support ticked with this account?

Edited:
I decided to post all the files as comments, you can recreate the sample project with them.
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:55 PM
<Window x:Class="carrouselSample.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:carousel="clr-namespace:Telerik.Windows.Controls.Carousel;assembly=Telerik.Windows.Controls.Navigation" 
    Title="Window1" Height="350" Width="535"   
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">  
    <Grid> 
        <Grid.Resources> 
            <carousel:ArithmeticValueConverter x:Key="ArithmeticValueConverter" /> 
 
            <Style TargetType="{x:Type telerik:CarouselItem}">  
                <Setter Property="Template">  
                    <Setter.Value> 
                        <ControlTemplate TargetType="{x:Type telerik:CarouselItem}">  
                            <Grid ClipToBounds="False" Height="150" Width="200">  
                                <Border RenderTransformOrigin="0.5, 1" ClipToBounds="False" 
                                        Width="{Binding ElementName=CarouselItemInnerGrid, Path=ActualWidth}" 
                                        Opacity="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.Opacity}" 
                                        Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.Visibility}" 
                                        BorderBrush="White" BorderThickness="0">  
                                    <Border.RenderTransform> 
                                        <TransformGroup> 
                                            <ScaleTransform ScaleX="{Binding RelativeSource={RelativeSource TemplatedParent},   
                                                        Path=ReflectionSettings.WidthOffset,   
                                                        Converter={StaticResource ArithmeticValueConverter},   
                                                        ConverterParameter=1}" ScaleY="{Binding RelativeSource={RelativeSource TemplatedParent},   
                                                        Path=ReflectionSettings.HeightOffset,   
                                                        Converter={StaticResource ArithmeticValueConverter},   
                                                        ConverterParameter=-1}"  
                                                    CenterY="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.OffsetY}" /> 
                                            <TranslateTransform  
                                                    X="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.OffsetX}" /> 
                                            <SkewTransform  
                                                    AngleX="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.Angle}" /> 
                                        </TransformGroup> 
 
                                    </Border.RenderTransform> 
                                    <Border.OpacityMask> 
                                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">  
                                            <LinearGradientBrush.GradientStops> 
                                                <GradientStop  
                                                        Offset="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ReflectionSettings.HiddenPercentage}" 
                                                        Color="Transparent" /> 
                                                <GradientStop Offset="1" Color="Black" /> 
                                            </LinearGradientBrush.GradientStops> 
                                        </LinearGradientBrush> 
                                    </Border.OpacityMask> 
                                    <Border.Background> 
                                        <VisualBrush  
                                                Visual="{Binding ElementName=CarouselItemInnerGrid}">  
                                        </VisualBrush> 
                                    </Border.Background> 
 
                                </Border> 
 
                                <Grid x:Name="CarouselItemInnerGrid">  
                                    <Border x:Name="CarouselItemMainBorder" Opacity="0.5" 
                                            BorderBrush="#FF91B3FF" BorderThickness="1,1,1,1" 
                                            CornerRadius="5,5,5,5" SnapsToDevicePixels="True">  
                                        <Border.Background> 
                                            <LinearGradientBrush EndPoint="108,472" 
                                                    StartPoint="108,23" MappingMode="Absolute">  
                                                <GradientStop Color="#FF2C3A68" Offset="0" /> 
                                                <GradientStop Color="#FF000000" Offset="1" /> 
                                                <GradientStop Color="#FF0F224C" Offset="0.045" /> 
                                                <GradientStop Color="#FF000000" Offset="0.0451" /> 
                                            </LinearGradientBrush> 
                                        </Border.Background> 
                                    </Border> 
                                    <Border Opacity="1" BorderBrush="#3F000000" 
                                            BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" 
                                            Margin="10,10,10,10" x:Name="CarouselItemInnerBorder" 
                                            SnapsToDevicePixels="True">  
                                        <Border.Background> 
                                            <LinearGradientBrush EndPoint="101,462" 
                                                    StartPoint="101,13" MappingMode="Absolute">  
                                                <GradientStop Color="#FF2C3A68" Offset="0" /> 
                                                <GradientStop Color="#FF000000" Offset="1" /> 
                                                <GradientStop Color="#FF0F224C" Offset="0.045" /> 
                                                <GradientStop Color="#FF000000" Offset="0.0451" /> 
                                            </LinearGradientBrush> 
                                        </Border.Background> 
                                        <ContentPresenter IsHitTestVisible="True" /> 
                                        <Border.Triggers> 
 
                                        </Border.Triggers> 
                                    </Border> 
                                </Grid> 
                            </Grid> 
 
                            <ControlTemplate.Triggers> 
                                <MultiTrigger> 
                                    <MultiTrigger.Conditions> 
                                        <Condition Property="IsSelected" Value="False" /> 
                                        <Condition Property="IsMouseOver" Value="True" /> 
                                    </MultiTrigger.Conditions> 
                                    <MultiTrigger.EnterActions> 
                                        <BeginStoryboard> 
                                            <Storyboard> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemMainBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[0].Color" 
                                                        To="#FF344B97" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemMainBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[2].Color" 
                                                        To="#FF233F7E" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemInnerBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[0].Color" 
                                                        To="#FF344B97" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemInnerBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[2].Color" 
                                                        To="#FF233F7E" Duration="0:0:0.3" /> 
                                            </Storyboard> 
                                        </BeginStoryboard> 
 
                                    </MultiTrigger.EnterActions> 
                                    <MultiTrigger.ExitActions> 
                                        <BeginStoryboard> 
                                            <Storyboard FillBehavior="Stop">  
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemMainBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[0].Color" 
                                                        To="#FF2C3A68" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemMainBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[2].Color" 
                                                        To="#FF0F224C" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemInnerBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[0].Color" 
                                                        To="#FF2C3A68" Duration="0:0:0.3" /> 
                                                <ColorAnimation  
                                                        Storyboard.TargetName="CarouselItemInnerBorder" 
                                                        Storyboard.TargetProperty="Background.GradientStops[2].Color" 
                                                        To="#FF0F224C" Duration="0:0:0.3" /> 
                                            </Storyboard> 
                                        </BeginStoryboard> 
                                    </MultiTrigger.ExitActions> 
                                </MultiTrigger> 
 
                                <Trigger Property="IsSelected" Value="True">  
                                    <Setter TargetName="CarouselItemMainBorder" 
                                            Property="Background">  
                                        <Setter.Value> 
                                            <LinearGradientBrush EndPoint="108,472" 
                                                    StartPoint="108,23" MappingMode="Absolute">  
                                                <GradientStop Color="#FF344B97" Offset="0" /> 
                                                <GradientStop Color="#FF000000" Offset="1" /> 
                                                <GradientStop Color="#FF233F7E" Offset="0.045" /> 
                                                <GradientStop Color="#FF000000" Offset="0.0451" /> 
                                            </LinearGradientBrush> 
                                        </Setter.Value> 
                                    </Setter> 
                                    <Setter TargetName="CarouselItemInnerBorder" 
                                            Property="Background">  
                                        <Setter.Value> 
                                            <LinearGradientBrush EndPoint="101,462" 
                                                    StartPoint="101,13" MappingMode="Absolute">  
                                                <GradientStop Color="#FF344B97" Offset="0" /> 
                                                <GradientStop Color="#FF000000" Offset="1" /> 
                                                <GradientStop Color="#FF233F7E" Offset="0.045" /> 
                                                <GradientStop Color="#FF000000" Offset="0.0451" /> 
                                            </LinearGradientBrush> 
                                        </Setter.Value> 
                                    </Setter> 
                                </Trigger> 
 
                            </ControlTemplate.Triggers> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
            </Style> 
 
            <Style TargetType="{x:Type telerik:CarouselDataRecordPresenter}">  
                <Setter Property="Template">  
                    <Setter.Value> 
                        <ControlTemplate TargetType="{x:Type telerik:CarouselDataRecordPresenter}">  
                            <Grid IsHitTestVisible="False" HorizontalAlignment="Stretch" 
                                         
                                    
                                    VerticalAlignment="Stretch">  
                                <Grid.Background> 
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">  
                                        <GradientStop Color="White" Offset="0"/>  
                                        <GradientStop Color="Silver" Offset="1"/>  
                                        <GradientStop Color="WhiteSmoke" Offset="0.43"/>  
                                        <GradientStop Color="WhiteSmoke" Offset="0.42"/>  
                                    </LinearGradientBrush> 
                                </Grid.Background> 
                                <Grid.RowDefinitions> 
                                    <RowDefinition Height="Auto" /> 
                                    <RowDefinition Height="Auto" /> 
                                </Grid.RowDefinitions> 
                                <StackPanel Grid.Row="0" Orientation="Vertical" Margin="5,5,0,0" 
                                        VerticalAlignment="Top">  
 
                                    <TextBlock  Text="{Binding Path=FullName}"  Height="Auto"   
                                            Foreground="Black"  FontSize="12" /> 
                                    <TextBlock Text="{Binding Path=FirstName}" Height="Auto"   
                                            Foreground="DarkBlue" FontSize="10" /> 
                                    <TextBlock Text="{Binding Path=LastName}" Height="Auto"   
                                                   TextTrimming="WordEllipsis" 
                                            Foreground="DarkGreen" FontSize="10" /> 
                                </StackPanel> 
                                  
                            </Grid> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
            </Style> 
 
        </Grid.Resources> 
          
          
        <telerik:RadCarousel Margin="10,10,0,41" Name="radCarousel1" 
                             ItemsSource="{Binding UserList}">  
              
        </telerik:RadCarousel> 
    </Grid> 
</Window> 
 
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:56 PM
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Navigation;  
using System.Windows.Shapes;  
using carrouselSample.Code;  
 
namespace carrouselSample  
{  
      
      
 
 
    /// <summary>  
    /// Interaction logic for Window1.xaml  
    /// </summary>  
    public partial class Window1 : Window  
    {  
        //this is just for demonstrating purposes  
        WindowVM _VM;  
 
        public Window1()  
        {  
            InitializeComponent();  
            Loaded += new RoutedEventHandler(Window1_Loaded);  
            //this is just for demonstrating purposes  
            _VM = new WindowVM();  
        }  
 
        void Window1_Loaded(object sender, RoutedEventArgs e)  
        {  
            //this is just for demonstrating purposes  
            this.DataContext = _VM;  
        }  
    }  
}  
 
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:57 PM
using System;  
using System.ComponentModel;  
using System.Windows.Threading;  
 
namespace carrouselSample.Code  
{  
    public class WindowVM: INotifyPropertyChanged  
    {  
 
        DispatcherTimer _AddUsersDispatcher;  
 
        /// <summary>  
        /// Add users async to simulate real world situation  
        /// </summary>  
        public WindowVM()  
        {  
            _AddUsersDispatcher = new DispatcherTimer();  
            _AddUsersDispatcher.Interval = new TimeSpan(0, 0, 0, 0, 300);  
            _AddUsersDispatcher.Tick += new EventHandler(_AddUsersDispatcher_Tick);  
            _AddUsersDispatcher.Start();  
            _UserList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_UserList_CollectionChanged);  
 
        }  
 
          
 
 
        /// <summary>  
        /// Add users  
        /// </summary>  
        void _AddUsersDispatcher_Tick(object sender, EventArgs e)  
        {  
 
            if (_UserList.Count < 30)  
            {  
                User u = new User() { FirstName = string.Format("John{0}", _UserList.Count), LastName = string.Format("{0}Smith", _UserList.Count), Other = DateTime.Now.ToString() };  
                _UserList.Add(u);  
            }  
            else   
            {  
                //stop the timer  
                _AddUsersDispatcher.Stop();  
            }  
             
        }  
 
 
 
        /// <summary>  
        /// The user list  
        /// </summary>  
        private ObservableSortableCollection<User> _UserList = new ObservableSortableCollection<User>();  
        public ObservableSortableCollection<User> UserList  
        {  
            get   
            {  
                return _UserList;  
            }  
 
        }  
 
 
        void _UserList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)  
        {  
            UserList.SortBy(c => c.FullName, ListSortDirection.Descending);  
        }
        #region INotifyPropertyChanged Members  
 
        public event PropertyChangedEventHandler PropertyChanged;  
        public void OnPropertyChanged(string property)  
        {  
            if (PropertyChanged != null)  
            {  
                PropertyChanged(thisnew PropertyChangedEventArgs(property));  
            }  
        }
        #endregion  
    }  
}  
 
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:58 PM
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
 
namespace carrouselSample.Code  
{  
    public class User  
    {  
 
        public string FirstName  
        {  
            get;  
            set;  
        }  
        public string LastName  
        {  
            get;  
            set;  
        }  
 
        public string Other  
        {  
            get;  
            set;  
        }  
 
        public string FullName  
        {  
            get 
            {  
                return string.Format("{0}, {1}",LastName,FirstName);  
            }  
        }  
    }  
}  
 
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 03:59 PM
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections.ObjectModel;  
 
namespace carrouselSample.Code  
{  
 
    // Summary:  
    //     Specifies the direction of a sort operation.  
    public enum ListSortDirection  
    {  
        Ascending = 0,  
        Descending = 1,  
    }  
 
    /// <summary>  
    /// Allows to create an observable + sortable collection  
    /// </summary>  
    /// <typeparam name="T">Type of colection</typeparam>  
    public class ObservableSortableCollection<T> : ObservableCollection<T>  
    {  
 
 
        List<T> _OriginalList;  
 
        /// <summary>  
        /// constructor  
        /// </summary>  
        public ObservableSortableCollection()  
            : base()  
        {  
 
        }  
 
 
        /// <summary>  
        /// Sort the observable collection  
        /// </summary>  
        /// <param name="keySelector">function used to sort</param>  
        /// <param name="direction">sort  direction</param>  
        public void SortBy<TKey>(Func<T, TKey> keySelector, ListSortDirection direction)  
        {  
            switch (direction)  
            {  
                case ListSortDirection.Ascending:  
                    SortCollection(Items.OrderBy(keySelector).ToList());  
                    break;  
                default:  
                    SortCollection(Items.OrderByDescending(keySelector).ToList());  
                    break;  
 
            }  
        }  
 
 
        /// <summary>  
        /// Sorts the collection  
        /// </summary>  
        /// <param name="sortedItems">sorted list</param>  
        private void SortCollection(IList<T> sortedItems)  
        {  
            //first clear the list, then add the items ordered  
            Items.Clear();  
            foreach (var item in sortedItems)  
            {  
                Items.Add(item);  
            }  
        }  
 
 
 
    }  
}  
 
0
Pedro
Top achievements
Rank 1
answered on 08 Mar 2010, 04:01 PM
You will be able to recreate the sample project with those posts, as I mentioned, that code works fine with q2 with minor changes to the carouselItem template.
0
Milan
Telerik team
answered on 10 Mar 2010, 01:32 PM
Hi Pedro,

Thank you for sharing your code. 

It turned out that the exception is caused by an incorrect behavior of the ObservableSortableCollection and WindowVM. Whever the UserList is changed it raises a CollectionChanged event which notifies all listeners like RadCarousel. Unfortunately before this event reaches RadCarousel the items of the ObservableSortableCollection are already sorted (with changed places) and as a result RadCarousel receives CollectionChanged event with wrong indices.

Probably the best approach is to use a ListCollectionView which has built-in sorting functionality. Moreover by using ListCollectionView you will not have to implement custom collections. The switch to ListCollectionView requires just a few changes.:

public class WindowVM : INotifyPropertyChanged
{
    DispatcherTimer _AddUsersDispatcher;
    ObservableCollection<User> data = new ObservableCollection<User>();
  
    /// <summary>  
    /// The user list  
    /// </summary>  
    private ListCollectionView _UserList;
    public ListCollectionView UserList
    {
        get
        {
            return _UserList;
        }
        private set
        {
            this._UserList = value;
        }
    }
  
    /// <summary>  
    /// Add users async to simulate real world situation  
    /// </summary>  
    public WindowVM()
    {
        // create sorted ListCollectionView with empty data
        this.UserList = new ListCollectionView(this.data);
        this.UserList.SortDescriptions.Add(new SortDescription("FullName", System.ComponentModel.ListSortDirection.Descending));
  
        _AddUsersDispatcher = new DispatcherTimer();
        _AddUsersDispatcher.Interval = new TimeSpan(0, 0, 0, 0, 300);
        _AddUsersDispatcher.Tick += new EventHandler(_AddUsersDispatcher_Tick);
        _AddUsersDispatcher.Start();
  
    }
  
    /// <summary>  
    /// Add users  
    /// </summary>  
    void _AddUsersDispatcher_Tick(object sender, EventArgs e)
    {
  
        if (this.data.Count < 30)
        {
            User u = new User() { FirstName = string.Format("John{0}", _UserList.Count), LastName = string.Format("{0}Smith", _Us
            this.data.Add(u);
        }
        else
        {
            //stop the timer  
            _AddUsersDispatcher.Stop();
        }
  
    }

Hope this helps.


All the best,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Pedro
Top achievements
Rank 1
answered on 10 Mar 2010, 03:10 PM
Thanks a lot, the old version of the carousel control was hiding my wrong approach of the problem.
Tags
Carousel
Asked by
Pedro
Top achievements
Rank 1
Answers by
Milan
Telerik team
Pedro
Top achievements
Rank 1
Share this question
or