ListView不显示新源

时间:2014-12-21 14:22:55

标签: c# wpf listview

我有一个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 + "");表示itemsource已更改。

2 个答案:

答案 0 :(得分:0)

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

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

            set
            {
                _items = value;

            }
        }

并将ListsView的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中填写该集合,但

答案 1 :(得分:0)

我可以在表单加载时设置源代码,并且能够从我的代码隐藏文件中更新它。我正在测试项目中发布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();
        }
    }
}