列表视图不显示新源

本文关键字:新源 显示 视图 列表 | 更新日期: 2023-09-27 18:35:04

我有一个ListView,我在运行时设置了源代码。但它不显示新源,换句话说,它不会"刷新"以显示新源:

private List<Item> items = new List<Item>();
  private void _LoadItems() {
                this.lvItems.ItemsSource = this.items;
                MessageBox.Show(this.items.Count + "");
            }
        }

这是列表视图的 XAML:

<ListView Margin="0" Name="lvItems" SelectionChanged="lvItems_SelectionChanged">
     <ListView.View>
         <GridView>
             <GridViewColumn Header="Description" Width="400" DisplayMemberBinding="{Binding Description}" />
             <GridViewColumn Header="Unit Price" Width="150" DisplayMemberBinding="{Binding UnitPrice}" />
             <GridViewColumn Header="Quantity" Width="150" DisplayMemberBinding="{Binding Quantity}" />
         </GridView>
     </ListView.View>
 </ListView>

为什么当我通过this.lvItems.ItemsSource = this.items;设置新源时显示新源时不刷新?

此外,MessageBox.Show(this.items.Count + "");显示项源已更改。

列表视图不显示新源

正确的方法是定义一个ObservableCollection属性,而不是List来保存数据,如下所示:

 private ObservableCollection<String> _items;
        public ObservableCollection<String> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
            }
        }

并将列表视图的itemSource绑定到该集合:

<ListView Margin="0" Name="lvItems" SelectionChanged="lvItems_SelectionChanged" ItemsSource="{Binding Items}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Description" Width="400" DisplayMemberBinding="{Binding Description}" />
                            <GridViewColumn Header="Unit Price" Width="150" DisplayMemberBinding="{Binding UnitPrice}" />
                            <GridViewColumn Header="Quantity" Width="150" DisplayMemberBinding="{Binding Quantity}" />
                        </GridView>
                    </ListView.View>
                </ListView>

现在在 LoadItms 中填充该集合不稳,

我可以在表单加载时设置源代码,并能够从我的代码隐藏文件更新它。我正在发布测试项目中的 XAML 和代码,以防万一它可以帮助您:

我的 XAML:

<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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Click="Button_Click">Set</Button>
        <ListView Margin="0" Grid.Row="1" Name="lvItems">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Description" Width="400" DisplayMemberBinding="{Binding Description}" />
                    <GridViewColumn Header="Unit Price" Width="150" DisplayMemberBinding="{Binding UnitPrice}" />
                    <GridViewColumn Header="Quantity" Width="150" DisplayMemberBinding="{Binding Quantity}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadItems0();
        }
        private void LoadItems0()
        {
            var items = new[]{
                 new{
                     Description = "Des1",
                     UnitPrice = "1"
                 }
             };
            this.lvItems.ItemsSource = items;
        }
        private void _LoadItems()
        {
            var items = new[]{
                 new{
                     Description = "Des1",
                     UnitPrice = "1"
                 },
                 new{
                      Description = "Des2",
                     UnitPrice = "2"
                 },
                 new{
                     Description = "Des3",
                     UnitPrice = "3"
                 },
             };
            this.lvItems.ItemsSource = items;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _LoadItems();
        }
    }
}