ComboBox Template issue with collapsed state

4 posts, 0 answers
  1. Sid
    Sid avatar
    52 posts
    Member since:
    Sep 2008

    Posted 25 Oct 2012 Link to this post


    I've found the code to override the template selection:DataTemplateSelector

    But the collapsed state of the combobox is not working.

    Here is the XAML:
    <telerik:RadComboBox x:Name="cboPickups"
    Command="{Binding PickupChangedCommand}" HorizontalAlignment="Stretch"
    CommandParameter="{Binding ElementName=cboPickups,Path=SelectedItem}" IsEnabled="{Binding CanSelectPickup}"
    ItemsSource="{Binding Pickups}"
    SelectedValuePath="CustomerId" SelectedValue="{Binding Path=SelectedPickup.Id, Mode=OneWay}" Margin="4">
    <telerik:RadComboBox.ItemTemplateSelector>
    <Helpers:CustomerComboTemplateSelector />
    </telerik:RadComboBox.ItemTemplateSelector>
    </telerik:RadComboBox>

    Here are the templates:
    <DataTemplate x:Key="CustomerComboCollapsed">
        <TextBlock Text="{Binding CompanyName}" />
    </DataTemplate>
     
    <DataTemplate x:Key="CustomerComboExpanded">
        <StackPanel>
            <TextBlock Text="{Binding CompanyName}" />
            <TextBlock Text="{Binding CityState}" />
        </StackPanel>
    </DataTemplate>

    Here is the template selector class:
    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        var presenter = (ContentPresenter) container;
     
        if (presenter != null && item != null)
        {
            if (presenter.TemplatedParent is ComboBox)
            {
                return presenter.FindResource("CustomerComboCollapsed") as DataTemplate;
            }
            else
            {
                return presenter.FindResource("CustomerComboExpanded") as DataTemplate;
            }
     
        }
        return null;
    }

    But when the window initializes and it tries to load the ComboBox, the "container" is null.

    So it's trying to assign the collapsed template, but for some reason the container is null. Any ideas?  I've narrowed it down to the RadComboBox.  If I replace it with the base ComboBox control it works fine.

    I've attached the screen shots for the collapsed/expanded and the debugger.

    Thanks,
    -Sid.

  2. Boyan
    Admin
    Boyan avatar
    758 posts

    Posted 31 Oct 2012 Link to this post

    Hi Sid,

    We have different implementation of the Microsoft control and that is why this is not working as expected. We have our way to set template to the closed RadComboBox. You should use the SelectionBoxTemplate as shown in this example (3rd ComboBox). This way you will not need TemplateSelector at all just ItemTemplate and SlectionBoxTemplate.


    Kind regards,
    Boyan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Sid
    Sid avatar
    52 posts
    Member since:
    Sep 2008

    Posted 31 Oct 2012 Link to this post

    Boyan,

    Excellent!  So much easier, just needed to know how.

    Thanks,
    -Sid.
  5. Piotr
    Piotr avatar
    1 posts
    Member since:
    Aug 2016

    Posted 04 Sep in reply to Sid Link to this post

    SelectionBoxTemplate allows to choose only one template. What is items collection consists of object of different types? My traditional solution has a set of templates with names {TYPENAME}_ComboCollapsed and {TYPENAME}_ComboExpaned. Its easy to find proper data template using ItemTemplateSelector. 

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (container == null)
        {
            // Telerik ???
            return null;
        }
        var presenter = container as ContentPresenter;
        if (presenter == null)
        {
            // ???
            return null;
        }
        var templateName = presenter.TemplatedParent is ComboBox
            ? "Collapsed"
            : "Expanded";
        templateName = string.Format("{0}_Combo{1}", item.GetType().Name, templateName);
        return GetTemplateByName(templateName, presenter);
    }

Back to Top