当列表太长时,列表框中的数据模板内的容器为空
我有一个列表框,其中包含一个数据模板,其中包含 Image 和 TextBlock 控件。我想通过首先获取网格容器来获取 ImageControl
<ListBox x:Name="NewsList" Margin="0,0,20,0" SelectionChanged="NewsList_SelectionChanged" />
<DataTemplate>
<Grid Width="400" Height="89">
<Image HorizontalAlignment="Left" Width="64" x:Name="ImageThumbnail" Height="64" VerticalAlignment="Top" Margin="0,10,0,0" Source="http://vnexpress.net/Files/Subject/3B/A2/3B/15/top.jpg"/>
<TextBlock Text="{Binding Title}" Margin="78,0,8,0" TextWrapping="Wrap" FontSize="26.667" Height="74" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
</ListBox>
for (int i = 0; i < feeds.Count; i++)
var containerItem = list.ItemContainerGenerator.ContainerFromIndex(i);
当我设置 NewsList.Items = Feeds 且 feeds.Count 大于 23 时,某些 containerItem 为空。如果它小于 23,我永远不会得到空容器。
有谁知道原因是什么以及如何解决它。如果我们可以有另一种方法来获取 imageControl
I have a listbox contains a datatemplate with Image and TextBlock control inside. I want to get the ImageControl out by first getting the Grid Container
<ListBox x:Name="NewsList" Margin="0,0,20,0" SelectionChanged="NewsList_SelectionChanged" />
<DataTemplate>
<Grid Width="400" Height="89">
<Image HorizontalAlignment="Left" Width="64" x:Name="ImageThumbnail" Height="64" VerticalAlignment="Top" Margin="0,10,0,0" Source="http://vnexpress.net/Files/Subject/3B/A2/3B/15/top.jpg"/>
<TextBlock Text="{Binding Title}" Margin="78,0,8,0" TextWrapping="Wrap" FontSize="26.667" Height="74" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
</ListBox>
for (int i = 0; i < feeds.Count; i++)
var containerItem = list.ItemContainerGenerator.ContainerFromIndex(i);
And when I set the NewsList.Items = Feeds with feeds.Count is more than 23, some of the containerItem is null. If it is less than 23, I never get a null container.
Does anyone know what is the cause and how I can fix it. If we can have another way to get the imageControl out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到的一件事是你的 xaml 对我来说看起来不正确。
它不应该像这样吗:
除此之外,我不确定这是否会对您有帮助。
One thing I can see is that your xaml doesn't look right to me.
Shouldn't it look like this:
Beyond that I'm not sure if that will totally help you or not.
我的答案是编写一个新的 ImageConverter ,我们可以将其放入 xaml 文件中的标记中,我不必再处理 VisualTree 了,所以它可以工作
My answer is to write a new ImageConverter that we can put into the tag in the xaml file, I don't have to deal with VisualTree anymore, so it works