我找到了一个解决方案:
LongListMultiSelector LLMS = new LongListMultiSelector();
LLMS.ItemTemplate = CreateDataTemplate();
LLMS.ItemsSource = ExampleList;
浏览我的 LongListMultiSelector 中的所有项目:
int number = 0;
for(int i; i<ExampleList.Count; i++)
{
number = 0;
StackPanel sp = FindElementInVisualTree<StackPanel>(LLMS, i);
sp.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(StackPanel_Tap);
}
此方法在具有索引的父元素中查找元素:
private T FindElementInVisualTree<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 (number == ind)
{
number = 0;
return (T)child;
}
number++;
}
else
{
var result = FindElementInVisualTree<T>(child, ind);
if (result != null)
return result;
}
}
return null;
}
private DataTemplate CreateDataTemplate()
{
string xaml =
@"<DataTemplate
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<StackPanel>
<TextBlock Text='{Binding Title}'/>
...
...
</StackPanel>
</DataTemplate>";
DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);
return dt;
}