简体   繁体   English

wp7检查枢轴控件中的现有pivotitem

[英]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. 我想在pivot控件中动态创建一个pivotitem,但在我这样做之前,我想检查一下具有相同名称值的pivotitem是否已经存在。 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. 如果它不存在,我将按如下方式创建pivotitem并使其成为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. 我看到了pivot控件的Items.Contains(object)方法,但我不确定如何传递已经存在或可能不存在的对象。 Is there a way to just check if the Items collection has a pivotitem with a particular name? 有没有办法只检查Items集合是否有一个具有特定名称的pivotitem?

You could use LINQ to query the Items collection: 您可以使用LINQ查询Items集合:

bool contains = pivot
    .Items
    .Cast<PivotItem>()
    .Any((i) => i.Name == name);
if (!contains)
{
    // Add new PivotItem.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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