ListView没有更新itemssource ObservableCollection Item属性更改

时间:2015-01-27 11:53:40

标签: c# wpf mvvm data-binding

请考虑以下示例代码:

view.xml用

<Grid>
    <ListView Name="NameList" HorizontalAlignment="Left" Height="142" Margin="55,45,0,0" VerticalAlignment="Top" Width="389">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding FirstName}"/>
                    <Label Content="{Binding LastName}" Grid.Column="1"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="Button" HorizontalAlignment="Left" Margin="120,256,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

</Grid>

View.xml.cs

public partial class MainWindow : Window
{
    ViewModel vm;
    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel();
        this.DataContext = vm;
        NameList.ItemsSource = vm.fn;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        vm.fn.Add(new fullname("P", "Q"));
        vm.fn[0].FirstName = "NewName";
    }

}

ViewModel.cs

class ViewModel : INotifyPropertyChanged
{

    public ViewModel()
    {
        fn = new ObservableCollection<fullname>();
        fn.CollectionChanged += ContentCollectionChanged;
        fn.Add(new fullname("A", "B"));
        fn.Add(new fullname("C", "D"));
    }

    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (fullname item in e.OldItems)
            {
                //Removed items
                item.PropertyChanged -= EntityViewModelPropertyChanged;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (fullname item in e.NewItems)
            {
                //Added items
                item.PropertyChanged += EntityViewModelPropertyChanged;
            }
        }
    }

    public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        //This will get called when the property of an object inside the collection changes
    }

    public ObservableCollection<fullname> _fn;
    public ObservableCollection<fullname> fn
    {
        get { return _fn; }
        set { _fn = value; OnPropertyChanged("fn"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {

        }
    }
}

Model.cs

class fullname : INotifyPropertyChanged
{
    public fullname(string f, string l)
    {
        FirstName = f;
        LastName = l;
    }

    public string _FirstName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; OnPropertyChanged("FirstName"); }
    }


    public string _LastName;
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; OnPropertyChanged("LastName"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {

        }
    }
}

如果我在ObservableCollection中添加或删除任何项目,它会正确更新View中的ListView,但问题是如果我修改了ObservableCollection项属性,则ListView不会更新。

代表: 点击上面指定的按钮就可以了 一个。添加新项目(在ListView中成功反映)
湾修改第一个项目的FirstName(未反映在ListView中)

我该怎么做才能使修改反映在View中。

如果有人能指出我做错了什么,我会非常感激。

1 个答案:

答案 0 :(得分:4)

更改项目时,只有在添加,删除或移动项目时才会触发ObservableCollection事件。 您必须在项类中实现INotifyPropertyChanged接口,并在集合中注册每个项事件。

请参阅here

如果您对集合进行了大量更改,则可以提出

  

NotifyCollectionChangedAction.Reset

重置所有事件的事件

请参阅here