简体   繁体   中英

wp7 check for existing pivotitem in a pivot control

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM