Reputation: 83
I want to dynamically create a pivotitem inside a pivot control, but before I do that, I want to check if a pivotitem with the same name value doesn't exist already. Is there a way to do it? If it doesn't exist already, I would create the pivotitem as follows and make it the selecteditem.
p = new PivotItem();
p.Name = name;
p.Header = name;
pivot.Items.Add(p);
pivot.SelectedItem = p;
I see the pivot control's Items.Contains(object) method, but I am not sure how I would pass the object which may or may not exist already. Is there a way to just check if the Items collection has a pivotitem with a particular name?
Upvotes: 1
Views: 761
Reputation: 16319
You could use LINQ to query the Items
collection:
bool contains = pivot
.Items
.Cast<PivotItem>()
.Any((i) => i.Name == name);
if (!contains)
{
// Add new PivotItem.
}
Upvotes: 4