如何在 WinRT Metro 应用程序中将 ListView 绑定到 ListView 内部
How to bind ListView inside ListView in WinRT Metro application
我的应用程序中有两个 ListView。
这是我的模特 class
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 集合对象
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 根本没有绑定。需要帮助我做错了什么。
假设页面绑定到您可以使用此绑定的视图模型
ItemsSource="{Binding SurveyProductDetails}"
我会将 SurveyProductDetails 设置为只读 属性 您确保在调用 get 时支持字段不为空。
当您创建一个绑定到 ObservableCollection 的 ListBox 时,该列表中的每个项目都会被赋予一个数据上下文,它是集合中的对应对象。在这种情况下,每个顶级 ListBoxItem 都绑定到 SurveyDetailViewModel 类型的对象。
因此,您的 ItemsSource 只需:
{Binding SurveyProductDetails}
surveyDetail 可以省略。
我的应用程序中有两个 ListView。 这是我的模特 class
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 集合对象
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 根本没有绑定。需要帮助我做错了什么。
假设页面绑定到您可以使用此绑定的视图模型
ItemsSource="{Binding SurveyProductDetails}"
我会将 SurveyProductDetails 设置为只读 属性 您确保在调用 get 时支持字段不为空。
当您创建一个绑定到 ObservableCollection 的 ListBox 时,该列表中的每个项目都会被赋予一个数据上下文,它是集合中的对应对象。在这种情况下,每个顶级 ListBoxItem 都绑定到 SurveyDetailViewModel 类型的对象。
因此,您的 ItemsSource 只需:
{Binding SurveyProductDetails}
surveyDetail 可以省略。