GridViewColumn未在ListView中订阅PropertyChanged事件

时间:2010-04-07 14:44:52

标签: wpf .net-3.5 listview inotifypropertychanged

我有一个带有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事件。我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

找出问题所在。 “订阅”列表来自LINQ查询。当我将.ToList()添加到该查询的末尾时,UI开始正确更新。

相关问题