我在一个app上工作,它有一个pivot,每个pivot项都有一个LongListMultiSelector。两者都适用于数据绑定。我想使用LongListMultiselector的选定项。我发现了这个:
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
我把它编辑到了这个:
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement, int ind) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
if (LonglistSelectorNumber == ind)
{
LonglistSelectorNumber = 0;
return (T)child;
}
LonglistSelectorNumber++;
}
else
{
var result = FindFirstElementInVisualTree<T>(child, ind);
if (result != null)
return result;
}
}
return null;
}
但是如果我从我的Pivot向我的List添加了3个以上的项目,我的方法会给我一个null-Reference。
我需要另一种方法来查找我的Pivot中的所有LongListMultiSelector。
我的支点:
<phone:Pivot x:Name="WorkoutPivot" Title="Workouts" ItemsSource="{Binding WorkoutList}" SelectionChanged="WorkoutPivot_SelectionChanged" >
<phone:Pivot.Background>
<ImageBrush ImageSource="/WorkoutNote;component/Assets/PanoramaBackground.png"/>
</phone:Pivot.Background>
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<StackPanel>
...
</StackPanel>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<toolkit:LongListMultiSelector Margin="-5,0,0,0" ItemsSource="{Binding ExercizeList}" FontFamily="Portable User Interface" AllowDrop="False">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,-6,0,12" Tap="StackPanel_Tap">
...
</StackPanel>
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>