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

ComboBox Template issue with collapsed state

3 Answers 52 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Sid
Top achievements
Rank 1
Sid asked on 25 Oct 2012, 11:14 PM

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.

3 Answers, 1 is accepted

Sort by
0
Boyan
Telerik team
answered on 31 Oct 2012, 08:58 AM
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.

0
Sid
Top achievements
Rank 1
answered on 01 Nov 2012, 03:50 AM
Boyan,

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

Thanks,
-Sid.
0
Piotr
Top achievements
Rank 1
answered on 04 Sep 2016, 07:51 AM

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);
}

Tags
ComboBox
Asked by
Sid
Top achievements
Rank 1
Answers by
Boyan
Telerik team
Sid
Top achievements
Rank 1
Piotr
Top achievements
Rank 1
Share this question
or