本文介绍了wp8 LongListSelector 项添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 WP8 的新手,我需要在 ItemTemplate 的 StackPanel 中添加一些随机控件,但不起作用.
I'm newbie on WP8, I need to add some random controls inside a ItemTemplate's StackPanel, but not work.
我的代码为:
<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 错误.
I've tryed to inject the new components on event ItemRealized, but I've a error of NullExecption when i get the StackPanel on Item.
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/[email protected]", UriKind.RelativeOrAbsolute));
imgBox.Children.Add(img);
}
}
感谢大家阅读我.
推荐答案
你可以做到在 binding
中提供 ImageSource
.
you can do that Providing ImageSource
in its binding
.
XAML:
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image ... />
<TextBlock ... />
<TextBlock ... />
<Image ... />
<StackPanel x:Name="ImgContainer">
<ListBox ItemsSource="{Binding ImgCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding imgPath}"></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
CS:
class ImgCollection
{
public string imgPath { get; set; }
public ImgCollection() { }
public ImgCollection(string imgPath)
{
this.imgPath = imgPath;
}
}
List<ImgCollection> obj = new List<ImgCollection>();
obj.Add(new ImgCollection("img path 1"));
obj.Add(new ImgCollection("img path 2"));
obj.Add(new ImgCollection("img path 3"));
obj.Add(new ImgCollection("img path 4"));
TripResultsData.ItemSource = obj;
这篇关于wp8 LongListSelector 项添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!