в том же примере выше, вам нужно определить имя привязки «ViewModelPropertyname» в классе
Пример: имя класса как «Модель»
int _PropertyNameWithinObject;
public int PropertyNameWithinObject
{
get
{
return PropertyNameWithinObject;
}
set
{
PropertyNameWithinObject= value;
OnPropertyChanged("PropertyNameWithinObject");
}
}
Включить следующий класс вКласс «Модель»
public class ViewModelBaseEx : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Затем определите коллекцию в другом классе с именем «ViewModel»
ObservableCollection<Model> _ViewModelPropertyName= new ObservableCollection<Model>();
public ObservableCollection<Model> ViewModelPropertyName
{
get
{
return _ViewModelPropertyName;
}
set
{
_ViewModelPropertyName= value;
OnPropertyChanged("ViewModelPropertyName");
}
}
для следующих привязок
<ListBox ItemsSource={Binding ViewModelPropertyName}" />
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text={Binding PropertyNameWithinObject} />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
иназначьте класс "ViewModel" в datacontext этой страницы в c # или дизайне, здесь я объявил в c # странице, как
this.DataContext = ViewModel;