【问题标题】:ListView ObservableCollection bindingListView ObservableCollection 绑定
【发布时间】:2014-01-14 23:11:12
【问题描述】:

我在将 Collection 绑定到 ListView 时遇到问题。

 public static ObservableCollection<ParagonViewClass> mparagonViewList = new ObservableCollection<ParagonViewClass>();
        public ObservableCollection<ParagonViewClass> paragonViewList
        {
            get
            {
                return mparagonViewList;
            }
        }

在方法中,当用户添加新项目时,我将其添加到列表中:

paragonViewList.Insert(0, newPar);

还尝试了 mparagonViewList.Insert(0, newPar);

xaml 文件中的项目源:

<ListView Grid.Row="1" Name="paragonListView1" ItemsSource="{Binding paragonViewList}" .../>

@EDIT:Listview 有 DataTemplate(带有标签的网格 - 我很确定绑定没问题,因为它只需设置 myListVIew.ItemsSource = myLis;)

看起来当我单击要添加到列表视图的产品时,它确实插入到数据库中,但我在列表视图中看不到该产品。可能有一点愚蠢的问题,但我真的找不到它;)

感谢您的任何回答!

【问题讨论】:

    标签: c# wpf listview binding


    【解决方案1】:

    查看您提供的代码,很难弄清楚您做错了什么,如果有的话。因此,我整理了一个可以运行的小示例应用程序(无论如何从 WPF 的角度来看)。

    我的模型叫ItemModel,而不是ParagonViewClass,定义如下

    public class ItemModel
    {
        public ItemModel() { }
        public string Text { get; set; }
    }
    

    我的 Xaml 是

    <Window x:Class="StackOverflow._20799346.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:common="clr-namespace:StackOverflow.Common;assembly=StackOverflow.Common"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                <Button Content="Add Item" Click="AddItem_OnClick" />
            </StackPanel>
            <ListView ItemsSource="{Binding Path=Items}">
                <ListView.ItemTemplate>
                    <DataTemplate DataType="{x:Type common:ItemModel}">
                        <TextBlock Text="{Binding Path=Text}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </DockPanel>
    </Window>
    

    注意DataContext 绑定到RelativeSource Self,允许后面的代码用作ViewModel。我通常更喜欢创建一个单独的 ViewModel 类,但这种方法有其优势,因为可以直接从控件中将事件发送到 ViewModel,而不是使用命令。

    后面的代码,现在是视图模型,看起来像

    public partial class MainWindow : Window
    {
        private ObservableCollection<ItemModel> items;
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        public ObservableCollection<ItemModel> Items { get { return items ?? (items = new ObservableCollection<ItemModel>()); } }
    
        private void AddItem_OnClick(object sender, RoutedEventArgs e)
        {
            Items.Add(new ItemModel() { Text = Items.Count.ToString(CultureInfo.CurrentCulture) });
        }
    }
    

    我在 Items 属性上使用了延迟加载技术。它只会在被访问时被实例化。为简单起见,当单击“添加项目”按钮时,我添加了一个新项目,其文本设置为 Items 集合的计数。

    您应该能够将此代码粘贴到新的 WPF 应用程序项目中,修复 xaml 文件中的命名空间并运行它。

    现在,正如上面 Rohit Vats 所暗示的,Items 属性不需要 Setter。当通过它实现的 INotifyPropertyChanged 和 INotifyCollectionChanged 接口添加或删除项目时,ObservableCollection 本身会通知 WPF 绑定子系统。

    我知道这并不能直接回答您的问题,但是如果没有关于原始问题的更多信息(即代码),就不可能知道出了什么问题。

    希望这个例子有所帮助。

    注意:为简洁起见,我删除了异常管理。

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 2018-05-21
      • 2017-09-29
      • 2011-08-27
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      相关资源
      最近更新 更多