【问题标题】:ObservableCollection doesn't update ListViewObservableCollection 不更新 ListView
【发布时间】:2017-01-21 19:52:00
【问题描述】:

我的 ObservableCollection 列表没有更新视图。我使用 MVVM Light

这是我的虚拟机

public class MainViewModel : ViewModelBase{
public ObservableCollection<ProductModel> Products { get; set; }

private void GetData()
{
    //getting data here

    Products = new ObservableCollection<ProductModel>();

    foreach (var item in myData)
    {
        Products.Add(item);
    }
}}

XAML:

DataContext="{Binding Source={StaticResource Locator}, Path=Main}"><FlowDocumentReader BorderBrush = "Black" BorderThickness="2">
<FlowDocument>
    <BlockUIContainer>
        <ListView BorderThickness = "2" ItemsSource="{Binding Products}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header = "Lp." DisplayMemberBinding="{Binding Path=OrdinalNumber}" Width="100"/>
                    <GridViewColumn Header = "id" DisplayMemberBinding="{Binding Path=id}" Width="100"/>
                    <GridViewColumn Header = "Name" DisplayMemberBinding="{Binding Path=Name}" Width="100"/>
                    <GridViewColumn Header = "Quantity" DisplayMemberBinding="{Binding Path=Quantity}" Width="100"/>
                    <GridViewColumn Header = "NetPrice" DisplayMemberBinding="{Binding Path=NetPrice}" Width="100"/>
                    <GridViewColumn Header = "GrossPrice" DisplayMemberBinding="{Binding Path=GrossPrice}" Width="100"/>
                    <GridViewColumn Header = "TotalCost" DisplayMemberBinding="{Binding Path=TotalCost}" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
    </BlockUIContainer>
</FlowDocument>

对我来说看起来不错。不知道问题出在哪里

【问题讨论】:

    标签: c# wpf xaml mvvm mvvm-light


    【解决方案1】:

    这不是 ObservableCollection 的问题,而是设置 Products 属性。

    Products = new ObservableCollection<ProductModel>();
    

    您在 GetData 方法中设置它,但您从未通知视图有关它,因此它没有绑定到您的 ObservableCollection。 您可以改为在构造函数中设置属性,或者在属性上也使用 INotifyPropertyChanged。

    【讨论】:

    • 咳咳,我现在明白了。我整天坐着做这个项目。我需要新鲜空气
    【解决方案2】:

    我看到您已经从 ViewModelBase 继承,因此您不需要这样做:

    public event PropertyChangedEventHandler PropertyChanged;
    
        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    相反,您可以这样做:

    public class MainViewModel : ViewModelBase{
    public ObservableCollection<ProductModel> Products { get; set; }
    
    private void GetData()
    {
        //getting data here
    
        Products = new ObservableCollection<ProductModel>();
    
        foreach (var item in myData)
        {
            Products.Add(item);
        }
        RaisePropertyChanged(nameof(Products)); // you are missing this
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2014-11-02
      • 2019-10-25
      • 2016-04-22
      相关资源
      最近更新 更多