0

I wish to update my listview in an elegant way.

This is my code:

ObservableCollection<Track> tracks = new ObservableCollection<Track>();
Track track = new Track();
Tracks.AddRange(track); // collection add a track, and list view updates too
Task.Factory.StartNew(() =>
    track.GetInfo(); // this operation might require some times to update
    // here I need to notify that my object is updated
);

How i can force to update binding of an object inside my ObservableCollection ?

This is my userControl.xaml

<UserControl.Resources>
    <local:TimeConverter x:Key="timeConverter" />
    <local:IndexConverter x:Key="indexConverter" />
    <CollectionViewSource x:Key="trackList" Source="{Binding Path=TrackList.Tracks}"/>
</UserControl.Resources>

<DockPanel LastChildFill="True">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" Margin="2, 5, 2, 5">
        <Button Name="btn_addtrack" Command="{Binding AddTrack}" Content="+" />
    </StackPanel>
    <ListView Name="lv_tracklist" 
              DataContext="{StaticResource trackList}" 
              ItemsSource="{Binding}"
              >
        <ListView.View>
            <GridView>
                <GridViewColumn Header="#" Width="20" DisplayMemberBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Converter={StaticResource indexConverter}}" />
                <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}"/>
                <GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}"/>
                <GridViewColumn Header="Length" DisplayMemberBinding="{Binding Length, Converter={StaticResource timeConverter}}" />
                <GridViewColumn Header="Path" DisplayMemberBinding="{Binding Location}"/>
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>
user1649247
  • 47
  • 2
  • 7
  • Can you post the XAML binding the `ListView` to the `tracks` collection? – vlad Sep 20 '12 at 13:03
  • Does Track class implement INotifyPropertyChanged? Can you post some code of it? – michele Sep 20 '12 at 13:03
  • No, Track does not implement INotifyPropertyChanged, anyway I don't need to notify the update of each single property. I need the update of ALL the properties after the call of track.GetInfo(); above – user1649247 Sep 20 '12 at 13:22
  • You have not accepted a single answer and based on you comments you have at least one answer. You do know to check an answer? – paparazzo Sep 20 '12 at 13:59
  • Unlucky my reputation is < of 15 and I can't vote up :( – user1649247 Sep 20 '12 at 23:14

2 Answers2

4

If you are opposed to implementing INotifyPropertyChanged you could set the binding to null then rebind but that is not what I would reccomend.

See this answer for notify all

notify-all-properties-of-the-view-model-has-changed

Unless you are changing all the properties I would go with the answer from Miklos. There is overhead to NotifyPropertyChanged.

Recommended pattern

   private string prop1

   Public string Prop1
   {
        get { return prop1; }
        set 
        {
            if (prop1 == value) return;
            prop1 = value;
            NotifyProperyChanged("Prop1");
        }
   {

Yes lines of code but then the UI only paints what has changed.

I just saw the update to your question and your comment.
An update to ALL is an update to each property.
The UI receives notification at the property level.
You only have 5 properties.

Community
  • 1
  • 1
paparazzo
  • 44,497
  • 23
  • 105
  • 176
2

Your Track class needs to implement the INotifyPropertyChanged interface.

Raise the PropertyChanged event in the setter of every property (after you set the new value) you want to send notification.

Miklós Balogh
  • 2,164
  • 2
  • 19
  • 26
  • This is a bit expansive talking about lines coding, sure doesn't exists a way to notify the entire change of all object's properties ? – user1649247 Sep 20 '12 at 13:12
  • You can call OnNotifyPropertyChanged("") to force all bound properties to re-validate. However, the datacontext must implement INotifyPropertyChanged. – Michael G Sep 20 '12 at 13:25
  • Ok, I've decided to make a ViewModel for Track, then I'll try to figure out how to implement it in the cheaper way. I'll make you see my implementation and then I'll put here. Thanks for all the answers – user1649247 Sep 20 '12 at 23:10