【发布时间】:2014-06-16 21:02:53
【问题描述】:
我是 WP8 的新手,我需要在 ItemTemplate 的 StackPanel 中添加一些随机控件,但不起作用。
我的代码为:
<phone:LongListSelector
x:Name="TripResultsData"
SelectionChanged="TripResultsData_SelectionChanged"
ItemRealized="TripResultsData_ItemRealized"
IsGroupingEnabled="False"
Grid.Column="0"
Grid.Row="1">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image ... />
<TextBlock ... />
<TextBlock ... />
<Image ... />
<StackPanel x:Name="ImgContainer">
<!-- Here I need to add n images -->
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
我尝试在事件 ItemRealized 上注入新组件,但是当我在 Item 上获取 StackPanel 时出现 NullExecption 错误。
private void TripResultsData_ItemRealized(object sender, ItemRealizationEventArgs e)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
StackPanel imgBox = (StackPanel)e.Container.FindName("ImgContainer");
Image img = new Image();
img.Source = new BitmapImage(new Uri("/Assets/images/ico_eurocity_obbdb@2x.png", UriKind.RelativeOrAbsolute));
imgBox.Children.Add(img);
}
}
感谢大家阅读我。
【问题讨论】:
-
请通过这个Link。最好的方法是使用 XAML 进行数据绑定。就像 User368316 向您描述的那样。
-
非常感谢@Eldho,感谢您的帮助!
标签: windows-phone-8 longlistselector