简体   繁体   English

WPF ListView 未更新

[英]WPF ListView not updating

I have got a list view and bind ItemSource to public ObservableCollection StockList { get;我有一个列表视图并将 ItemSource 绑定到 public ObservableCollection StockList { get; set;放; }. }. I initiated the StoclList in the Constructor method.我在 Constructor 方法中启动了 StoclList。 I am getting filtered stock list from a ViewModel and adding these items to this StcokList but somehow ListView doesn't show the list items.我正在从 ViewModel 获取过滤后的库存列表并将这些项目添加到此 StcokList 但不知何故 ListView 不显示列表项目。

But if i set ItemsSource="{Binding ListFromViewModel}" It works..但是如果我设置 ItemsSource="{Binding ListFromViewModel}" 它有效..

 public partial class MainWindow : Window     {   

    public MainWindowViewModel viewModel;
    public ObservableCollection<StockModel> StockList { get; set; }

    //Constructor
    public MainWindow()
    {
        StockList = new ObservableCollection<StockModel>();
        InitializeComponent();
        viewModel = new MainWindowViewModel();
        this.DataContext = viewModel;           
    }

    private async void BtnSelectFile_Click(object sender, RoutedEventArgs e)
    {
        GetData();
        viewModel.StartProcessing(progress);
        StockList.Clear();
        for (int i = 0; i < viewModel.ListOfStocks.Count; i++)
        {
            StockList.Add(viewModel.ListOfStocks[i]);
        }
    }

And my XAML file is like this而我的 XAML 文件是这样的

<ListView Grid.Row="1" Grid.Column="1" x:Name="LvErrorList" ItemsSource="{Binding StockList}">

UPDATE: If i use this code, it start working.更新:如果我使用此代码,它就会开始工作。 But I am not sure why!!!但我不知道为什么!!!

LvErrorList.ItemsSource = StockList;

When you type {Binding StockList} in your XAML markup, WPF will try to bind to the StockList property of the current DataContext of the ListView .当您在 XAML 标记中键入{Binding StockList}时,WPF 将尝试绑定到ListView当前DataContextStockList属性。

Since the ListView inherits its DataContext from the parent element all the way up to the parent window, it will look for the property in the MainWindowViewModel object which you have set as the DataContext in the constructor of the window.由于 ListView 从父元素一直继承它的DataContext一直到父窗口,它会在MainWindowViewModel对象中查找您在窗口的构造函数中设置为DataContext的属性。

If you set the DataContext the the instance of a class where the StockList property is defined, ie the window itself in this case, your binding will work:如果您将DataContext设置为定义StockList属性的类的实例,即在这种情况下是窗口本身,您的绑定将起作用:

this.DataContext = this;

Alternatively, you could move the StockList property to the view model or opt out of the DataContext by specifying an explicit source for the binding.或者,您可以将StockList属性移动到视图模型或通过为绑定指定显式源来选择退出DataContext This will bind to the StockList property of the window itself regardless of the DataContext :这将绑定到窗口本身的StockList属性,而不管DataContext

<ListView Grid.Row="1" Grid.Column="1" x:Name="LvErrorList"
    ItemsSource="{Binding StockList, RelativeSource={RelativeSource AncestorType=Window}}">

Please refer to the docs for more information about data binding in WPF and how it works.有关 WPF 中的数据绑定及其工作原理的更多信息,请参阅文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM