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

How to Set EmptySelectionBoxTemplate in code?

2 Answers 88 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Bob asked on 08 Aug 2011, 07:21 PM
I have a RadRibbonComboBox in a RadRibbonTab in a RadRibbonBar.  Looks something like this:

<telerik:RadRibbonBar>
    <telerik:RadRibbonTab Header="Tab 2" x:Name="Tab2">
        <telerik:RadRibbonGroup Header="Select an item">
            <StackPanel Orientation="Vertical" Margin="0,5,0,0">
                <telerik:RadRibbonComboBox Name="myComboBox" Margin="0,5,0,0" >
                    <telerik:RadRibbonComboBoxItem Content="New Item..." Selected="NewItem_Selected"></telerik:RadRibbonComboBoxItem>
                </telerik:RadRibbonComboBox>
            </StackPanel>
        </telerik:RadRibbonGroup>
    </telerik:RadRibbonTab>
</telerik:RadRibbonBar>

I have defined three different Data Templates in the resources like this:

<UserControl.Resources>
    <DataTemplate x:Key="EmptyTemplate1">
        <TextBlock FontStyle="Italic" Text="Select a value from the list" />
    </DataTemplate>
    <DataTemplate x:Key="EmptyTemplate2">
        <TextBlock FontStyle="Italic" Text="There are no selections at this time" />
    </DataTemplate>
    <DataTemplate x:Key="EmptyTemplate3">
        <TextBlock FontStyle="Italic" Text="Please complete the first tab before selecting" />
    </DataTemplate>
</UserControl.Resources>

I want to use a specific Data Template for the EmptySelectionBoxTemplate depending on various conditions.  Therefore, I want to set the EmptySelectionBoxTemplate in code.  I have tried setting the template like this:

myComboBox.EmptySelectionBoxTemplate = (DataTemplate)FindResource("EmptyTemplate1");

The resource is found, and appears to be set in the combo box, but the display does not get altered.  I also tried invalidating the property and visual state:

myComboBox.InvalidateProperty(RadComboBox.EmptySelectionBoxTemplateProperty);
myComboBox.InvalidateVisual();

Invalidating did not help.  The combo box selection is -1, and if I set the EmptySelectionBoxTemplate in XAML, the box displays the message correctly.

What is the correct way to change this template in code?

2 Answers, 1 is accepted

Sort by
0
Accepted
Yana
Telerik team
answered on 09 Aug 2011, 12:42 PM
Hello Bob,

You should reset the ComboBox template, please try it like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    comboBox1.SelectedIndex = -1;
    comboBox1.EmptySelectionBoxTemplate = (DataTemplate)FindResource("EmptyTemplate1");
    this.ResetTemplate();
}
 
private void ResetTemplate()
{
    var temp = comboBox1.Template;
    comboBox1.Template = null;
    comboBox1.Template = temp;
}

and let us know the result.

Kind regards,
Yana
the Telerik team

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

0
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
answered on 09 Aug 2011, 04:57 PM
Thank you, the reset code worked.
Tags
ComboBox
Asked by
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Yana
Telerik team
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Share this question
or