2016-04-02 12 views
1

Also habe ich dieses Problem, dass ich eine Listview habe und wenn ich das erste Element es rendert, aber die zweite nicht rendern, aber das Panel der Listview ist wächst, als ob es getan hätte. Danach wird, wenn ein drittes Element hinzugefügt wird, sowohl das zweite als auch das dritte Rendering hinzugefügt. Danach wird jedes hinzugefügte Element sofort gerendert. Dieses Problem tritt nur bei dem zweiten Element auf.Zweiter Eintrag in Listview rendern nicht das erste Mal richtig

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> 

hinter Code

Listenerstellung und Bindung:

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

Hinzufügen neuer Artikel:

wpfDownloads.Add(download); 

dank

Antwort

1

Verwenden Sie einfach ObservableCollection<FileDownload> statt List<FileDownload>. Ursache ObservableCollection<T> hat INotifyCollectionChanged Schnittstelle implementiert.

As MSDN says:

INotifyCollectionChanged benachrichtigen Hörer von dynamischen Änderungen, wie , wie wenn ein Element hinzugefügt und entfernt werden oder die gesamte Liste gelöscht wird.

Einfache Beratung über die Benutzeroberfläche:

Ich schlage vor, Sie resizable XAML zu erstellen, nicht statisch. Ich meine, es ist schlecht:

<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> 

Es ist jedoch besser, und dies UI (XAML) ist 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> 
Verwandte Themen