对象更新后通知PropertyChanged [英] Notify PropertyChanged after object update

查看:66
本文介绍了对象更新后通知PropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以一种优雅的方式更新我的列表视图.

I wish to update my listview in an elegant way.

这是我的代码:

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

我如何强制更新ObservableCollection中的对象绑定?

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

这是我的userControl.xaml

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>

推荐答案

如果您反对实现INotifyPropertyChanged,则可以将绑定设置为null,然后重新绑定,但这不是我所建议的.

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

除非您要更改所有属性,否则我将使用Miklos的答案. NotifyPropertyChanged有开销.

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

推荐模式

   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.

我刚刚看到了您的问题和评论的更新.
对ALL的更新就是对每个属性的更新.
UI在属性级别接收通知.
您只有5个属性.

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.

这篇关于对象更新后通知PropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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