简体   繁体   English

更换ObservableCollection时ListView不更新

[英]ListView not updating when ObservableCollection is replaced

I can not understand why the ListView is not refreshing when the ObservableCollection is replaced (with new one) and not changed (added or removed items). 我无法理解为什么在替换ObservableCollection(使用新的)并且未更改(添加或删除的项目)时不会刷新ListView。 I respected all requirements for property notifications, since I'm using a DependencyObject for my view model, and SetValue is called when the collection is replaced . 我尊重属性通知的所有要求,因为我正在为我的视图模型使用DependencyObject,并且在替换集合时调用SetValue。

I have a WPF ListView bound to a Col property of my view model: 我有一个WPF ListView绑定到我的视图模型的Col属性:

public class ViewModel1 : DependencyObject
{
    public ViewModel1()
    {
        Col = new ObservableCollection<string>(new[] { "A", "B", "C", "D" });
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        Debug.WriteLine("Property changed "+ e.Property.Name);
    }   

    public ObservableCollection<string> Col
    {
        get { return (ObservableCollection<string>)GetValue(ColProperty); }
        set { SetValue(ColProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ColProperty =
        DependencyProperty.Register("ColProperty", typeof(ObservableCollection<string>), typeof(ViewModel1), new PropertyMetadata(null));

}

The XAML is like: XAML就像:

<Window x:Class="BindingPOC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <ListView Margin="0,10,0,0" ItemsSource="{Binding Col}" />
            <Button Click="Button_Click" >click</Button>
        </StackPanel>

    </Grid>
</Window>

So with this code, everything work fine if I don't replace the initial ObservableCollection. 因此,使用此代码,如果我不替换初始的ObservableCollection,一切正常。 But when I click on the button. 但是当我点击按钮时。 I replace the list with: 我将列表替换为:

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            (DataContext as ViewModel1).Col = new System.Collections.ObjectModel.ObservableCollection<string>(new[] { "Z", "ZZ" });

        }

The PropertyChanged method on the view model is called for Col, but the ListView is not updating its content. 对于Col调用视图模型上的PropertyChanged方法,但ListView不更新其内容。

Do I need to conserve the same ObservableCollection reference ? 我是否需要保存相同的ObservableCollection引用? why ? 为什么?

This is because your dependency property registration is incorrect. 这是因为您的依赖项属性注册不正确。 The name of the property passed to the Register method should be "Col", not "ColProperty": 传递给Register方法的属性名称应为“Col”,而不是“ColProperty”:

public static readonly DependencyProperty ColProperty =
    DependencyProperty.Register("Col", typeof(ObservableCollection<string>), typeof(ViewModel1), new PropertyMetadata(null));

The initial binding works because there is a property named Col , but it is not detected as a dependency property, so the binding is not automatically updated. 初始绑定有效,因为有一个名为Col的属性,但它没有被检测为依赖属性,因此绑定不会自动更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定的ObservableCollection更改时,ListView不更新 - ListView not updating when the bound ObservableCollection changes 当更新绑定到Listview的ObservableCollection中的Item时,指定的强制转换无效 - Specified cast not Valid when updating Item in ObservableCollection bound to Listview Listview 不会根据 ObservableCollection 属性进行更新 - Listview is not updating based on ObservableCollection properties 更改ObservableCollection后ListView不更新 - ListView not updating after ObservableCollection is changed 替换ObservableCollection的项目时如何通知 - How to be notified when an item of an ObservableCollection is replaced "ListView 未更新 itemsource ObservableCollection 项目属性更改" - ListView not updating on the itemsource ObservableCollection Item Property change 使用ObservableCollection填充listview时出现问题 - Problem when filling listview with ObservableCollection 创建新的ObservableCollection时WPF ObservableCollection不更新DataGrid - WPF ObservableCollection not updating DataGrid when creating a new ObservableCollection 当项目被添加时,Observablecollection不更新列表 - Observablecollection not updating list, when an item gets added 调用add时,ObservableCollection依赖项属性未更新 - ObservableCollection dependency property not updating when add is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM