简单的ListView数据

简单的ListView数据

本文介绍了简单的ListView数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用WPF和C#来显示在的ListView 数据,我对我所看到的不同的例子和方法混淆。我在寻找类似我的程序,或pre-必需品清单,使其工作全面工作的例子。我会很高兴,如果我设法从我的收藏只显示1行数据。目前,该列表视图中显示任何内容。

I'm trying to display data in a ListView with WPF and C#, and I'm confused by the different examples and methods I have seen. I'm looking for a fully working example similar to my program, or a list of pre-requisites to make it work. I'll be happy if I manage to display just 1 row of data from my collection. Currently, the list view displays nothing.

C#:

public partial class MainWindow : Window
{
    public ObservableCollection<Row> Rows { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Rows = new ObservableCollection<Row>();
        Rows.Add(new Row
        {
            ID = "42",
            Category = "cat",
            CharLimit = 32,
            Text = "Bonjour"
        });
    }
}

public class Row
{
    public string ID { get; set; }
    public string Category { get; set; }
    public int CharLimit { get; set; }
    public string Text { get; set; }
}

XAML:

<ListView ItemsSource="{Binding Path=Rows}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="200" Header="ID" DisplayMemberBinding="{Binding Path=ID}" />
            <GridViewColumn Width="200" Header="Category" DisplayMemberBinding="{Binding Path=Category}" />
            <GridViewColumn Width="200" Header="Text" DisplayMemberBinding="{Binding Path=Text}" />
        </GridView>
    </ListView.View>
</ListView>

在此先感谢

推荐答案

创建一个视图模型可设置为XAML中的数据上下文

Create a viewmodel which can be set as the data context for the XAML

 public class WindowsViewModel
 {
    private ObservableCollection<RowViewModel> m_Rows;

    public ObservableCollection<RowViewModel> Rows
    {
        get { return m_Rows; }
        set { m_Rows = value; }
    }

    public WindowsViewModel()
    {
        Rows = new ObservableCollection<RowViewModel>();
        Rows.Add(new RowViewModel
            {
                ID = "42",
                Category = "cat",
                CharLimit = 32,
                Text = "Bonjour"
            });
    }
}


实现类 RowViewModel 在下面的方式:

 public class RowViewModel:INotifyPropertyChanged
    {
      public RowViewModel()
      {
      }
      private string m_ID;
      public string ID
      {
            get
            {
                return m_ID;
            }
            set
            {
                m_ID = value;
                NotifyPropertyChanged("ID");
            }
      }
      public string Category
      {
            get;
            set;
      }

      public int CharLimit
      {
        get;
        set;
      }

      public string Text
      {
        get;
        set;
      }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string Obj)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this,new PropertyChangedEventArgs(Obj));
        }
    }
}


在XAML背后的code,添加code:


In the code behind of XAML, add the code:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new WindowsViewModel();
        }
    }


添加更新源触发属性在ListView节点:


Add the update source trigger property in the listview node:

<ListView ItemsSource="{Binding Rows, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="200" Header="ID" DisplayMemberBinding="{Binding Path=ID}" />
                    <GridViewColumn Width="200" Header="Category" DisplayMemberBinding="{Binding Path=Category}" />
                    <GridViewColumn Width="200" Header="Text" DisplayMemberBinding="{Binding Path=Text}" />
                </GridView>
            </ListView.View>

这篇关于简单的ListView数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:20