Verint Verint
Verint Verint

Reputation: 567

WPF: how to auto update ObservableCollection inside ListView

I have my object collection:

public class Data
{
   string name {get; set;}
   int progres {get; set;}
}

public ObservableCollection<Data> dataFiles { get; set; }

And my ListView:

    <ListView Name="lvDataFiles" 
              ItemsSource="{Binding dataList}">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Foreground" Value="White"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.Resources>
            <DataTemplate x:Key="MyDataTemplate">
                <Grid Margin="-6">
                    <ProgressBar Name="prog" Maximum="100" Value="{Binding Progress}" 
                                 Width="{Binding Path=Width, ElementName=ProgressCell}" 
                                 Height="16" Margin="0" Foreground="#FF5591E8" Background="#FF878889" />
                    <TextBlock Text="{Binding Path=Value, ElementName=prog, StringFormat={}{0}%}" VerticalAlignment="Center"
                               HorizontalAlignment="Center" FontSize="11" Foreground="White" />
                </Grid>
            </DataTemplate>
            <ControlTemplate x:Key="ProgressBarTemplate">
                <Label  HorizontalAlignment="Center" VerticalAlignment="Center" />
            </ControlTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}">
                <GridViewColumn Width="425" Header="File name" DisplayMemberBinding="{Binding FileName}" />
                <GridViewColumn x:Name="ProgressCell"  Width="50" Header="Progress"
                                CellTemplate="{StaticResource MyDataTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>

My ListView has 2 columns: file name and Progress (that contain progress bar) My Data collection has the Progress property that changing every few seconds.

Is it possible that my ListView ProgressBar will update automatic each time specific object (or several in the same time..) changing ? Or i need to go over my collection and update ?

Upvotes: 1

Views: 789

Answers (1)

mdebeus
mdebeus

Reputation: 1928

Your Data class must inherit from INotifyPropertyChanged, add a NotifyPropertyChange method and call that for each setter.

public class Data : INotifyPropertyChanged
{
   private string _name;
   public string name
   {
      get { return _name; }
      set
      {
          _name= value;
          NotifyPropertyChanged("name");
      }
   }
   private int _progress;
   public int progress 
   {
      get { return _progress; }
      set
      {
          _progress = value;
          NotifyPropertyChanged("progress");
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   virtual public void NotifyPropertyChange( string propertyName )
   {
       var handler = PropertyChanged;
       if (handler != null)
           handler(this, new PropertyChangedEventArgs(propertyName));
   }
}

Upvotes: 3

Related Questions