WPF 应用程序中的 ListView 数据不会刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19164958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:46:53  来源:igfitidea点击:

ListView Data Won't Refresh in WPF App

c#wpfentity-frameworklistview

提问by Brodie

I am trying to load data into a ListView in WPF using a Stored Procedure and Entity Framework. When I first load the User Control the ListView loads the data just fine. I then call the same code to refresh the data and I can see through debugging that the ListItems count changes but the data on the front end screen does not update.

我正在尝试使用存储过程和实体框架将数据加载到 WPF 中的 ListView。当我第一次加载用户控件时,ListView 加载数据就好了。然后我调用相同的代码来刷新数据,我可以通过调试看到 ListItems 计数发生变化,但前端屏幕上的数据没有更新。

XAML

XAML

<ListView Name="DocsListView" ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Documents" DisplayMemberBinding="{Binding Documents}"/>
        </GridView>
    </ListView.View>
</ListView>

Code Behind

背后的代码

public void LoadDocs()
{
    Context _Context = new Context();
    DocsListView.ItemsSource = null;
    DocsListView.ItemsSource = _Context.SP_GetDocuments(1).ToList();
    _Context = null;
}

Can someone please help me figure out what I'm doing wrong? I'm using VS2012, .Net 4.5, and EF 5.0.

有人可以帮我弄清楚我做错了什么吗?我使用的是 VS2012、.Net 4.5 和 EF 5.0。

采纳答案by makim

I′m not sure whats causing the Problem, generally I would recommend to use a DependencyProperty!

我不确定是什么导致了问题,通常我会建议使用 DependencyProperty!

But you could try to refresh the ListView like that:

但是您可以尝试像这样刷新 ListView:

ICollectionView view = CollectionViewSource.GetDefaultView(DocsListView.ItemsSource);
view.Refresh();

If this is going to be a bigger Project I strongly recommend to have a look at the MVVM DesignPattern!

如果这将是一个更大的项目,我强烈建议您查看 MVVM DesignPattern!

WPF and MVVMplay well together. There are nice Libraries you can use like MVVMLight.

WPF 和 MVVM 配合得很好。您可以使用一些不错的库,例如MVVMLight

Also you should have a look at the INotifyPropertyChanged Interface.

你也应该看看INotifyPropertyChanged Interface

XAML-Code (Don′t forget to set the DataContext)

XAML-Code(不要忘记设置DataContext)

<Window x:Class="WpfApplication1.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"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <ListView Name="DocsListView" ItemsSource="{Binding Data}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Documents" DisplayMemberBinding="{Binding Documents}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

C# Code-Behind (ObservableCollection refreshes the UI automatically if a Item gets added or deleted):

C# 代码隐藏(如果添加或删除项目,ObservableCollection 会自动刷新 UI):

public ObservableCollection<YourEntity> Data
{
     get { return (ObservableCollection<YourEntity>)GetValue(DataProperty); }
     set { SetValue(DataProperty, value); }
}

// Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(ObservableCollection<YourEntity>), typeof(MainWindow), null);

public void LoadDocs()
{
     Context _Context = new Context();
     if(Data == null)
     {
         Data = new ObservableCollection<YourEntity>();
     }
     else
     {
         Data.Clear();
     }
     foreach(var doc in _Context.SP_GetDocuments(1).ToList())
     {
         Data.Add(doc);
     }
     _Context = null;
}