This question is locked. New answers and comments are not allowed.
Hello,
I was wondering what is the best way to see whether a RadComboBoxItem exists inside a RadComboBox?
I can get the ComboBox.Items collection but I'm not seeing a method for determining whether an item that we are looking for is already in the list.
Thanks.
I was wondering what is the best way to see whether a RadComboBoxItem exists inside a RadComboBox?
I can get the ComboBox.Items collection but I'm not seeing a method for determining whether an item that we are looking for is already in the list.
Thanks.
3 Answers, 1 is accepted
0
Hi Rory,
You could use a simple Linq expression to check whether there is a certain RadComboBoxItem (or other object) present in the RadComboBox.Items collection:
using System.Linq;
...
bool exists = combo.Items.Any(item => (item as RadComboBoxItem).Content == "value");
if (exists)
{
...
}
Greetings,
Valeri Hristov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
You could use a simple Linq expression to check whether there is a certain RadComboBoxItem (or other object) present in the RadComboBox.Items collection:
using System.Linq;
...
bool exists = combo.Items.Any(item => (item as RadComboBoxItem).Content == "value");
if (exists)
{
...
}
Greetings,
Valeri Hristov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Chase
commented on 03 May 2023, 01:32 PM
| edited
Top achievements
Rank 1
How are you doing a Linq query against an ItemCollection collection type? I literally just pasted this line of code into my sandbox test app to see if this works (and it does not as you can see below, unless there is more than one RadComboBox?).
Here's the error as proof. How are you making this work?
Martin Ivanov
commented on 05 May 2023, 06:47 AM
Telerik team
You can cast the Items collection to the corresponding item type. For example:
comboBox.Items.OfType<RadComboBoxItem>().Any(x => x.Content.Equals("value"));
Note that if the ItemsSource is populated with a collection of custom objects, the Items collection will contain these elements. In that case, you can use the corresponding data type in the OfType<T> method, and the data model property in the conditional statement.
comboBox.Items.OfType<MyItemInfo>().Any(x => x.MyContentProperty.Equals("value"));
0

Labcorp
Top achievements
Rank 1
answered on 06 Jan 2014, 04:59 PM
Using a similar example, How do you retrieve the index of something that exists
0
Hello Labcorp,
In order to find the index of the desired Item you could use again a simple Linq expression. Please check the following code snippet:
Hope this helps.
Regards,
Kalin
Telerik
In order to find the index of the desired Item you could use again a simple Linq expression. Please check the following code snippet:
var item = this.Combo.Items.FirstOrDefault(i => (i as RadComboBoxItem).Content.ToString() == "value");
if (item != null)
{
int indexOfItem = this.Combo.Items.IndexOf(item);
}
Hope this helps.
Regards,
Kalin
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>