列表视图中的第二项无法正确呈现第一次 [英] Second item in listview doesn't render the first time properly
问题描述
所以我有一个问题,我有一个列表视图,当我添加第一个项目时,它会渲染,但是第二个不能渲染,但是列表视图所在的面板就像它一样在增长.然后,如果添加了第三项,则第二个和第三个渲染器都将添加.之后,添加的每个新项目都会立即渲染.此问题仅与第二项有关.
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.