It refers primarily to the ObservableCollection<T>
properties. The Public method with a dummy Private variable
need to specify the DataContext via XAML, for example:
ObservableCollection<T>
public class ViewModel : NotificationObject
{
private ObservableCollection<Person> _myCollection;
public ObservableCollection<Person> MyCollection
{
get
{
return _myCollection;
}
set
{
_myCollection = value;
NotifyPropertyChanged("MyCollection");
}
}
}
XAML
<!-- Set the DataContext in XAML -->
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding Path=MyCollection}" /> <!-- Or any other control -->
</Grid>
Work with collection in code-behind like this:
ViewModel MyViewModel = this.DataContext as ViewModel;
MyViewModel.MyCollection = new ObservableCollection<Person>();
MyViewModel.MyCollection.Add(new Person()
{
Age = 22,
Name = "Nick",
});
In this case, if the collection would have been with automatic property:
public ObservableCollection<Person> MyCollection
{
get;
set;
}
Then Binding
would not work, namely the it would not update the property collection. But, if you set the DataContext
in code-behind:
this.DataContext = MyViewModel;
The Binding
will work with automatic property.