RadComboBox set text when item not in ItemsSource list

1 Answer 18 Views
ComboBox
Paul
Top achievements
Rank 1
Iron
Paul asked on 20 May 2022, 11:55 AM

I have a RadComboBox with a list of items in ItemsSource
My SelectedValue is an int property and I display the corresponding string property from the list.

If the SelectedValue property is not in the list then it does not display anything.

Is there a way to set the display text in the RadComboBox when the SelectedValue is not in the ItemsSource list?

I've tried setting Text but it didn't work.

(note the RadComboBox is not editable)

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 25 May 2022, 08:32 AM

Hello Paul,

fIndeed, the SelectedValue must correspond to an item in the ItemsSource for the selection to be performed and the corresponding text to be displayed.

To display a string which does not correspond to an item in the list, you can manually get ahold of the TextBlock responsible for displaying the selection and change its Text property manually:

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            var tb = this.combo.ChildrenOfType<TextBlock>().First();
            tb.SetCurrentValue(TextBlock.TextProperty, "Test");
        }

For your convenience, I've prepared a small sample project which demonstrates this approach.

Alternatively, you can add a dummy item to the ItemsSource and set it as the SelectedItem if this would be a suitable approach for you.

Please let me know if any of these two approaches would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ComboBox
Asked by
Paul
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or