我有一个带有 GridView 的 ListView,它绑定到实现 INotifyPropertyChanged 的类的属性,如下所示:
<ListView Name="SubscriptionView" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" ItemsSource="{Binding Path=Subscriptions}">
<ListView.View>
<GridView>
<GridViewColumn Width="24" CellTemplate="{StaticResource IncludeSubscriptionTemplate}"/>
<GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Name}" Header="Subscription"/>
<GridViewColumn Width="75" DisplayMemberBinding="{Binding Path=RecordsWritten}" Header="Records"/>
<GridViewColumn Width="Auto" CellTemplate="{StaticResource FilenameTemplate}"/>
</GridView>
</ListView.View>
</ListView>
该类如下所示:
public class Subscription : INotifyPropertyChanged
{
public int RecordsWritten
{
get
{
return _records;
}
set
{
_records = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("RecordsWritten"));
}
}
private int _records;
...
}
所以我启动了一个 BackgroundWorker 并开始写入记录,更新 RecordsWritten 属性并期望 UI 中的值发生变化,但事实并非如此。事实上,Subscription 对象上的 PropertyChanged 的值为 null。这是一个谜,因为我认为 WPF 应该订阅实现 INotifyPropertyChanged 的数据对象的 PropertyChanged 事件。我在这里做错了吗?