How to Set EmptySelectionBoxTemplate in code?

3 posts, 1 answers
  1. Bob
    Bob avatar
    59 posts
    Member since:
    Aug 2010

    Posted 08 Aug 2011 Link to this post

    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. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 09 Aug 2011 Link to this post

    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 >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Bob
    Bob avatar
    59 posts
    Member since:
    Aug 2010

    Posted 09 Aug 2011 Link to this post

    Thank you, the reset code worked.
Back to Top