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
Create a viewmodel
which can be set as the data context for the XAML
public class WindowsViewModel
{
private ObservableCollection m_Rows;
public ObservableCollection Rows
{
get { return m_Rows; }
set { m_Rows = value; }
}
public WindowsViewModel()
{
Rows = new ObservableCollection();
Rows.Add(new RowViewModel
{
ID = "42",
Category = "cat",
CharLimit = 32,
Text = "Bonjour"
});
}
}
Implement the class RowViewModel
in the below fashion:
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:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new WindowsViewModel();
}
}
Add the update source trigger property in the listview node: