Simple ListView Data Binding

I am trying to display data in a ListView with WPF and C #, and I am confused by the various examples and methods that I have seen. I am looking for a fully working example similar to my program, or a list of prerequisites to make it work. I will be glad if I can only display one row of data from my collection. Currently, the list view does not display anything.

WITH#:

 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> 

Thank you in advance

+4
source share
2 answers

Create a viewmodel that can be set as a data context for 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 class as follows:

  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 XAML code, add the code:

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

Add the trigger property of the update source to 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> 

+4
source

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

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

Similarly, you indicate that it should look for Rows in the current Window

+1
source

Source: https://habr.com/ru/post/1485225/


All Articles