C# 简单的 ListView 数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17003998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:20:19  来源:igfitidea点击:

Simple ListView data binding

c#wpflistviewdata-bindingbinding

提问by Noxxys

I'm trying to display data in a ListViewwith 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.

我正在尝试ListView使用 WPF 和 C#显示数据,但我对所看到的不同示例和方法感到困惑。我正在寻找一个类似于我的程序的完整工作示例,或使其工作的先决条件列表。如果我设法只显示收藏中的 1 行数据,我会很高兴。目前,列表视图不显示任何内容。

C#:

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:

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>

Thanks in advance

提前致谢

采纳答案by Arushi Agrawal

Create a viewmodelwhich can be set as the data context for the XAML

创建一个viewmodel可以设置为 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"
            });
    }
}


Implement the class RowViewModelin the below fashion:

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));
        }
    }
}


In the code behind of XAML, add the code:

在 XAML 背后的代码中,添加以下代码:

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


Add the update source trigger property in the listview node:

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

<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>

回答by dkozl

You must specify source otherwise, like in your case, it will look for the property in current context which by default, if nothing else is specified, will be DataContext. Try something like this:

您必须指定 source 否则,就像在您的情况下一样,它将在当前上下文中查找属性,如果没有指定其他内容,默认情况下将为DataContext. 尝试这样的事情:

<ListView ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Rows}">

Like that you specify that it should look for Rowsin current Window

就像你指定它应该Rows在当前查找Window