using System.Collections.ObjectModel;
using System.Windows;
using System.ComponentModel;
namespace Test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Ставим источник данных для формы. Сам класс ViewModel чуть ниже.
this.DataContext = new ViewModel();
}
}
/// <summary>
/// Источник данных для формы
/// </summary>
class ViewModel : ViewModelBase
{
public ObservableCollection<ListViewRow> _Source;
public ViewModel()
{
_Source = new ObservableCollection<ListViewRow>();
_Source.Add(new ListViewRow { BrandName = "Dodge", BrandCountry = "USA", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "FAW", BrandCountry = "China", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "Ferrari", BrandCountry = "Italy", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "Fiat", BrandCountry = "Italy", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "VAZ", BrandCountry = "Russia", BrandGroup = "Russian brands" });
_Source.Add(new ListViewRow { BrandName = "Kamaz", BrandCountry = "Russia", BrandGroup = "Russian brands" });
}
/// <summary>
/// Свойство - будет ItemsSource для ListView
/// </summary>
public ObservableCollection<ListViewRow> TableWithData
{
get { return _Source; }
}
}
/// <summary>
/// Одна строка с данными
/// </summary>
public class ListViewRow
{
public string BrandName { get; set; }
public string BrandCountry { get; set; }
public string BrandGroup { get; set; }
}
/// <summary>
/// Вспомогательный класс для создания всех ViewModel
/// </summary>
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
|