列表视图中的第二项无法正确呈现第一次 [英] Second item in listview doesn't render the first time properly

查看:71
本文介绍了列表视图中的第二项无法正确呈现第一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题,我有一个列表视图,当我添加第一个项目时,它会渲染,但是第二个不能渲染,但是列表视图所在的面板就像它一样在增长.然后,如果添加了第三项,则第二个和第三个渲染器都将添加.之后,添加的每个新项目都会立即渲染.此问题仅与第二项有关.

So I have this problem that I have a listview and when I add the first item it renders, but the second one doesn't render but the panel the listview is on grows as if it did. Afterwards if a third item is added both the second and the third render. After that, each new item added renders instantly. This problem is only with the second item.

XAML:

<ListView Name="DownloadList" Background="WhiteSmoke" PreviewMouseLeftButtonUp="DownloadList_PreviewMouseLeftButtonUp" Width="649" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.View>                  
        <GridView>
            <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="80" Header="Status" DisplayMemberBinding="{Binding Status}"/>
            <GridViewColumn Width="60" Header="Size" DisplayMemberBinding="{Binding FormattedSize}"/>
            <GridViewColumn Width="70" Header="Speed" DisplayMemberBinding="{Binding FormattedSpeed}"/>
            <GridViewColumn Width="170" Header="Progress">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                            <Grid>
                                <ProgressBar Name ="dlPrgrs" Height ="20" Width="150" Maximum="100" Value="{Binding FormattedProgress}" Foreground="{Binding ForColor}"/>
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=dlPrgrs, Path=Value, StringFormat={}{0:0}%}"/>
                            </Grid>
                        </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="65" Header="Time Left" DisplayMemberBinding="{Binding FormattedTime}"/>
            <GridViewColumn Width="60" Header="Peers" DisplayMemberBinding="{Binding Peers}"/>
         </GridView>
     </ListView.View>
</ListView>   

隐藏代码

列表创建和绑定:

List<FileDownload> wpfDownloads = new List<FileDownload>();
DownloadList.ItemsSource = wpfDownloads;

添加新项目:

wpfDownloads.Add(download);

谢谢

推荐答案

只需使用ObservableCollection<FileDownload>而不是List<FileDownload>. 原因ObservableCollection<T>已实现INotifyCollectionChanged接口.

Just use ObservableCollection<FileDownload> instead of List<FileDownload>. Cause ObservableCollection<T> has implemented INotifyCollectionChanged interface.

INotifyCollectionChanged通知侦听器动态变化,例如 例如添加或删除某项或清除整个列表时.

INotifyCollectionChanged notifies listeners of dynamic changes, such as when an item is added and removed or the whole list is cleared.

有关您的用户界面的简单建议:

我建议您创建可调整大小的XAML,而不是静态的.我的意思是不好:

I suggest you to create you resizable XAML, not static. I mean it is bad:

<Grid>        
   <ListView HorizontalAlignment="Left" Margin="10">
       ...The code omitted for the brevity...
   </ListView>
   <Button Margin="50" Name="btn_add" Click="btn_add_Click" Content="Add new item"/>
</Grid>

但是,更好,并且此UI(XAML)可调整大小:

However, it is better and this UI(XAML) is resizable:

<Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="5*">
      </RowDefinition>
    </Grid.RowDefinitions>  
    <ListView HorizontalAlignment="Left">
       ...The code omitted for the brevity...
    </ListView>
    <Button Grid.Row="1" Name="btn_add" Click="btn_add_Click" Content="Add new item"/>
</Grid>

这篇关于列表视图中的第二项无法正确呈现第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆