我的应用程序中有两个ListView。 这是我的Model类
public class SurveyDetailViewModel : INotifyPropertyChanged
{
private int _RoomTypeId;
private string _RoomType;
public int RoomTypeId
{
get { return _RoomTypeId; }
set
{
_RoomTypeId = value;
NotifyPropertyChanged("RoomTypeId");
}
}
public string RoomType
{
get { return _RoomType; }
set
{
_RoomType = value;
NotifyPropertyChanged("RoomType");
}
}
public ObservableCollection<SurveyProductDetails> SurveyProductDetails { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class SurveyProductDetails : INotifyPropertyChanged
{
private Guid _RoomId;
private int _ProductTypeId;
private string _Title;
private string _Color;
private int _Quantity;
private int _QuantityInstalled;
private string _RoomDescription;
private string _ProductDescription;
private string _ProductSpecification;
public Guid RoomId
{
get { return _RoomId; }
set
{
_RoomId = value;
NotifyPropertyChanged("RoomId");
}
}
public int ProductTypeId
{
get { return _ProductTypeId; }
set
{
_ProductTypeId = value;
NotifyPropertyChanged("ProductTypeId");
}
}
public string Title
{
get { return _Title; }
set
{
_Title = value;
NotifyPropertyChanged("Title");
}
}
public string Color
{
get { return _Color; }
set
{
_Color = value;
NotifyPropertyChanged("Color");
}
}
public string ProductSpecification
{
get { return _ProductSpecification; }
set
{
_ProductSpecification = value;
NotifyPropertyChanged("ProductSpecification");
}
}
public string ProductDescription
{
get { return _ProductDescription; }
set
{
_ProductDescription = value;
NotifyPropertyChanged("ProductDescription");
}
}
public int Quantity
{
get { return _Quantity; }
set
{
_Quantity = value;
NotifyPropertyChanged("Quantity");
}
}
public int QuantityInstalled
{
get { return _QuantityInstalled; }
set
{
_QuantityInstalled = value;
NotifyPropertyChanged("QuantityInstalled");
}
}
public string RoomDescription
{
get { return _RoomDescription; }
set
{
_RoomDescription = value;
NotifyPropertyChanged("RoomDescription");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我有一个Observable Collection对象
ObservableCollection<SurveyDetailViewModel> surveyDetail = new ObservableCollection<SurveyDetailViewModel>();
获取数据。我试图将外部ListView的ItemSource设置为surveyDetail
ItemsSource="{Binding surveyDetail}"
和内在的这样
ItemsSource="{Binding surveyDetail.SurveyProductDetails}"
我也试过这个内心的
ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=SurveyProductDetails}"
ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent},Path=surveyDetail.SurveyProductDetails}"
外部ListView工作正常,但内部ListView根本没有绑定。需要帮助我做错了。
答案 0 :(得分:1)
假设页面绑定到viewmodel,您可以使用此绑定
ItemsSource="{Binding SurveyProductDetails}"
我会使SurveyProductDetails成为一个只读属性,确保在调用get时后备字段不为null。
答案 1 :(得分:0)
当您创建一个绑定到ObservableCollection的ListBox时,该列表中的每个项都会被赋予一个datacontext,它是集合中的相应对象。在这种情况下,每个顶级ListBoxItem都绑定到SurveyDetailViewModel类型的对象。
因此,您的ItemsSource只需要:
{{1}}
surveyDetail可以省略。