我不确定是什么导致了问题,通常我会建议使用 DependencyProperty!
但是您可以尝试像这样刷新 ListView:
ICollectionView view = CollectionViewSource.GetDefaultView(DocsListView.ItemsSource);
view.Refresh();
如果这将是一个更大的项目,我强烈建议您查看 MVVM DesignPattern!
WPF 和 MVVM配合得很好。您可以使用一些不错的库,例如MVVMLight。
你也应该看看INotifyPropertyChanged Interface。
XAML 代码(不要忘记设置 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# 代码隐藏(如果添加或删除项目,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;
}