ResourcesTypes sort problem

2 posts, 0 answers
  1. Robert
    Robert avatar
    15 posts
    Member since:
    Aug 2011

    Posted 11 Aug 2011 Link to this post

    Hello,
      I have a problem with random re-sorting of ResourceTypes collection. In my scenario I have two ResourceTypes in ResourceTypeCollection inserted dynamically at runtime. First ResourceTypes contains user names, second contains row numbers.

    User A 
      - Row 1
      - Row 2
      - Row 3
    User B
      - Row 1
      - Row 2
      - Row 3
    etc.

    If I load dynamically at runtime new group of users (User C, User D), the second ResourceTypes (row numbers) are sorted wrong. But  this behavior is random - sometimes it happens after third or fourth load of users' group, sort is also random. Looks like this for example:
    User A
      - Row 2
      - Row 1
      - Row 3
    User B
      - Row 3
      - Row 2
      - Row 3

        I found it is caused by my CustomGroupHeaderContentTemplateSelector, but i don't know why.  I need ResourceTypes group headers oriented horizontally.
    This is my code

    CustomGroupHeaderContentTemplateSelector:
    using System.Windows;
    using Telerik.Windows.Controls;
      
    namespace ScheduleViewTest
    {
        public class CustomGroupHeaderContentTemplateSelector : ScheduleViewDataTemplateSelector
        {
            public DataTemplate PersonResourceTemplate { get; set; }
            public DataTemplate RowResourceTemplate { get; set; }
      
            public override DataTemplate SelectTemplate(object item, DependencyObject container, ViewDefinitionBase activeViewDeifinition)
            {
                string rType = ((Telerik.Windows.Controls.Resource)((System.Windows.Data.CollectionViewGroup)item).Name).ResourceType;
                if (rType == "Person")
                {
                    return this.PersonResourceTemplate;
                }
                else if (rType == "Row")
                {
                    return this.RowResourceTemplate;
                }
                return base.SelectTemplate(item, container, activeViewDeifinition);
            }
        }
    }


    XAML:
    <UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="ScheduleViewTest.MainPage"
        xmlns:local="clr-namespace:ScheduleViewTest"
        mc:Ignorable="d"
        d:DesignHeight="768" d:DesignWidth="1024">
      
        <UserControl.Resources>
            <local:CustomGroupHeaderContentTemplateSelector x:Key="GroupHeaderContentTemplateSelector">
                <local:CustomGroupHeaderContentTemplateSelector.PersonResourceTemplate>
                    <DataTemplate>
                        <Border Width="140" Margin="4 4 4 0">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding}" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </local:CustomGroupHeaderContentTemplateSelector.PersonResourceTemplate>
                <local:CustomGroupHeaderContentTemplateSelector.RowResourceTemplate>
                    <DataTemplate>
                        <Border Width="100" Margin="4 2 4 0" BorderThickness="0">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding}" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </local:CustomGroupHeaderContentTemplateSelector.RowResourceTemplate>
            </local:CustomGroupHeaderContentTemplateSelector>
      
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <telerik:RadComboBox Grid.Column="0" 
                                 HorizontalAlignment="Left" Margin="12,12,0,0" Name="radComboBox1" 
                                 VerticalAlignment="Top" Width="173" SelectedIndex="0" 
                                 SelectionChanged="radComboBox1_SelectionChanged">
                <telerik:RadComboBoxItem Content="Group 1" />
                <telerik:RadComboBoxItem Content="Group 2" />
                <telerik:RadComboBoxItem Content="Group 3" />
            </telerik:RadComboBox>
            <telerik:RadScheduleView Grid.Column="1"
                                     Name="ScheduleView"
                                     GroupHeaderContentTemplateSelector="{StaticResource GroupHeaderContentTemplateSelector}">
                <telerik:RadScheduleView.AppointmentsSource>
                    <TimelineCollection>
                    </TimelineCollection>
                </telerik:RadScheduleView.AppointmentsSource>
      
                <telerik:RadScheduleView.GroupDescriptionsSource>
                    <telerik:GroupDescriptionCollection>
                        <telerik:ResourceGroupDescription ResourceType="Person" />
                        <telerik:ResourceGroupDescription ResourceType="Row" />
                    </telerik:GroupDescriptionCollection>
                </telerik:RadScheduleView.GroupDescriptionsSource>
                <telerik:RadScheduleView.ViewDefinitions>
                    <telerik:TimelineViewDefinition Orientation="Horizontal" VisibleDays="7" Title="TimeLine" />
                </telerik:RadScheduleView.ViewDefinitions>
            </telerik:RadScheduleView>
        </Grid>
    </UserControl>

    Code-behind:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    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 Telerik.Windows.Controls;
      
    namespace ScheduleViewTest
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
      
                this.GenerateResourceTypes();
            }
      
            private ResourceTypeCollection _resourceTypes;
            public ResourceTypeCollection ResourcesTypes
            {
                get
                {
                    return _resourceTypes;
                }
                set
                {
                    _resourceTypes = value;
                }
            }
      
            private void GenerateResourceTypes()
            {
                ResourceType personType = new ResourceType("Person");
                switch (radComboBox1.SelectedIndex)
                {
                    case 0:
                        // Group 1
                        personType.Resources.Add(new Resource("User A"));
                        personType.Resources.Add(new Resource("User B"));
                        personType.Resources.Add(new Resource("User C"));
                        personType.Resources.Add(new Resource("User D"));
                        personType.Resources.Add(new Resource("User E"));
                        break;
      
                    case 1:
                        // Group 2
                        personType.Resources.Add(new Resource("User F"));
                        personType.Resources.Add(new Resource("User G"));
                        personType.Resources.Add(new Resource("User H"));
                        break;
      
                    case 2:
                        // Group 3 
                        personType.Resources.Add(new Resource("User I"));
                        personType.Resources.Add(new Resource("User J"));
                        personType.Resources.Add(new Resource("User K"));
                        personType.Resources.Add(new Resource("User L"));
                        break;
                }
      
                ResourceType rowType = new ResourceType("Row");
                rowType.Resources.Add(new Resource("Office 1"));
                rowType.Resources.Add(new Resource("Office 2"));
                rowType.Resources.Add(new Resource("Office 3"));
      
                if (this.ResourcesTypes == null)
                {
                    this.ResourcesTypes = new ResourceTypeCollection();
                }
                for (int i = ResourcesTypes.Count - 1; i >= 0; i--)
                {
                    this.ResourcesTypes[i].Resources.RemoveAll(p => p != null);
                    this.ResourcesTypes.RemoveAt(i);
                }
                this.ResourcesTypes.Add(personType);
                this.ResourcesTypes.Add(rowType);
      
                ScheduleView.ResourceTypesSource = this.ResourcesTypes;
            }
      
            private void radComboBox1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
            {
                if (radComboBox1 != null)
                {
                    this.GenerateResourceTypes();
                }
            }
        }
    }


    Could you please help me with this?

    See also attached pictures.

    Thanks
    Robert
  2. Robert
    Robert avatar
    15 posts
    Member since:
    Aug 2011

    Posted 12 Aug 2011 Link to this post

    I found the solution, wrong binding of TextBlock Text.
    Corrent binding has to be <TextBlock Text="{Binding Name}" /> 
      instead of <TextBlock Text="{Binding}" />

    <local:CustomGroupHeaderContentTemplateSelector x:Key="GroupHeaderContentTemplateSelector">
        <local:CustomGroupHeaderContentTemplateSelector.PersonResourceTemplate>
            <DataTemplate>
                <Border Width="140" Margin="4 4 4 0">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </local:CustomGroupHeaderContentTemplateSelector.PersonResourceTemplate>
        <local:CustomGroupHeaderContentTemplateSelector.RowResourceTemplate>
            <DataTemplate>
                <Border Width="30" Margin="4 2 4 0" BorderThickness="0">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </local:CustomGroupHeaderContentTemplateSelector.RowResourceTemplate>
    </local:CustomGroupHeaderContentTemplateSelector>
Back to Top