using System;
using System.Windows;
using System.Collections.ObjectModel;
namespace Test
{
public class Foo
{
static int _nextId;
public int Id { get; private set; }
public String Name { get; set; }
public ObservableCollection Bars { get; set; }
public Foo()
{
Id = _nextId++;
Name = String.Empty;
Bars = new ObservableCollection();
}
}
public class Bar
{
static int _nextId;
public int Id { get; private set; }
public String Name { get; set; }
public Bar()
{
Id = _nextId++;
Name = String.Empty;
}
}
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public ObservableCollection Foos { get; set; }
public MainWindow()
{
Foos = new ObservableCollection();
Foo newFoo;
for (int index = 0; index < 5; ++index)
{
newFoo = new Foo();
newFoo.Name = String.Format("Foo {0}", index);
Foos.Add(newFoo);
}
InitializeComponent();
DataContext = this;
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel
{
public ViewModel()
{
Foos = new ObservableCollection();
}
public ObservableCollection Foos { get; set; }
}
public class Foo : INotifyPropertyChanged
{
static int _nextId;
public int Id { get; private set; }
public ObservableCollection Bars { get; set; }
public Foo()
{
Id = _nextId++;
Name = String.Empty;
Bars = new ObservableCollection();
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
Notify("Name");
}
}
private void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Bar : INotifyPropertyChanged
{
static int _nextId;
public int Id { get; private set; }
public Bar()
{
Id = _nextId++;
Name = String.Empty;
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
Notify("Name");
}
}
private void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
DataContext="{Binding ElementName=gridTop, Path=SelectedItem.Bars}"