开发者

Container inside Datatemplate inside Listbox is null when list is too long

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


One thing I can see is that your xaml doesn't look right to me.

Shouldn't it look like this:

<ListBox x:Name="NewsList" Margin="0,0,20,0" SelectionChanged="NewsList_SelectionChanged">
  <ListBox.ItemTemplate>
    <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.ItemTemplate>
</ListBox>

Beyond that I'm not sure if that will totally help you or not.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜