TemplateSelector using ItemsSource.Count

3 posts, 0 answers
  1. Luke
    Luke avatar
    12 posts
    Member since:
    Mar 2011

    Posted 18 Sep 2011 Link to this post

    I'm trying to display different templates based on amount of items in observable collection.

    I have done the following

          public class SplitTemplateSelector : DataTemplateSelector
        {
            private int _count;
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                if (Count == 1)
                {
                    return SingleSplitTemplate;
                }
                else if (Count > 0)
                {
                    return MultiSplitTemplate;
                }
     
                return null;
            }
            public DataTemplate SingleSplitTemplate
            {
                get;
                set;
            }
            public DataTemplate MultiSplitTemplate
            {
                get;
                set;
            }
     
            public int Count { set; get; }
        }

    My XAML looks like:

       <telerik:ItemsControl
                                        ItemsSource="{Binding OriginatingIdtSplits}"
                                        VerticalAlignment="Stretch"
                                        HorizontalAlignment="Stretch">
                                        <telerik:ItemsControl.ItemTemplateSelector>
                                            <local:SplitTemplateSelector
                                                SingleSplitTemplate="{StaticResource SingleSplitTemplate}"
                                                MultiSplitTemplate="{StaticResource MultiSplitTemplate}"
                                                Count="{Binding OriginatingIdtSplits.Count}"
                                             />
                                        </telerik:ItemsControl.ItemTemplateSelector>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" MinWidth="400"/>
                                        </ItemsPanelTemplate>
                                    </telerik:ItemsControl>

    This throws me XML parsing exception. I tried binding observable collection directly and doing count on selector side as well, however it hasn't made any difference. Is there any way of implementing this behaviour with template selector or am I wasting my time?

    Thanks!
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 21 Sep 2011 Link to this post

    Hello Luke,

    Can you please share what exactly are you using the ItemsControl for ? Generally it is not intended for direct usage. It is a part of more complex Telerik controls. If I know a bit more about your scenario I may be able to point you to the right "Rad" prefixed control .

    As a side note - even if you insist on using this control , the approach stated above would not work . The reason :  The ItemTemplateSelector would receive only one item as a parameter and not the whole collection thus not giving access to the count property.

    Again if you can share the final behavior and look you are trying to achieve, I will check for any good way to achieve that with the right Telerik control.

    Regards ,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Luke
    Luke avatar
    12 posts
    Member since:
    Mar 2011

    Posted 21 Sep 2011 Link to this post

    Hi Pavel,

    I'm using ItemsControl as it gives me flexibility and it was quite easy to implement.
    I have a scenario where I need to display a list of accounts (with drop downs and some text fields in one row) - sort of like a normal list (but with custom data template).
    However, when there is only one item in observable collection It doesn't look like a list anymore, more like a form (since when there is only one account the logic is a lot different).

    I have partially solved the problem with a converter which also implements dependency properties (to hold datatemplates). I sort of a hybrid between template selector and converter. It works so far.

    public class SplitTemplateSelector : DependencyObject, IValueConverter
        {
            public static readonly DependencyProperty SingleSplitTemplateProperty =
                DependencyProperty.Register("SingleSplitTemplate",
                                            typeof(DataTemplate),
                                            typeof(SplitTemplateSelector),
                                            null);
     
            public static readonly DependencyProperty MultiSplitTemplateProperty =
                DependencyProperty.Register("MultiSplitTemplate",
                                            typeof(DataTemplate),
                                            typeof(SplitTemplateSelector),
                                            null);
     
            public DataTemplate SingleSplitTemplate
            {
                get { return (DataTemplate)GetValue(SingleSplitTemplateProperty); }
                set { SetValue(SingleSplitTemplateProperty, value); }
            }
     
            public DataTemplate MultiSplitTemplate
            {
                get { return (DataTemplate)GetValue(MultiSplitTemplateProperty); }
                set { SetValue(MultiSplitTemplateProperty, value); }
            }
     
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var splitCount = (int)value;
                return splitCount == 1 ? SingleSplitTemplate : MultiSplitTemplate;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException();
            }
     
            #endregion
        }
Back to Top