【问题标题】:How to bind ListView inside ListView in WinRT Metro application如何在 WinRT Metro 应用程序的 ListView 中绑定 ListView
【发布时间】:2015-11-15 23:53:43
【问题描述】:

我的应用程序中有两个 ListView。 这是我的模型课

  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>();

获取数据。我尝试将 Outer 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 根本没有绑定。需要帮助我做错了什么。

【问题讨论】:

    标签: listview windows-phone-8.1 windows-8.1 winrt-xaml


    【解决方案1】:

    假设页面绑定到视图模型,您可以使用此绑定

    ItemsSource="{Binding SurveyProductDetails}"
    

    我会将 SurveyProductDetails 设置为只读属性,确保在调用 get 时支持字段不为空。

    【讨论】:

      【解决方案2】:

      当您创建一个绑定到 ObservableCollection 的 ListBox 时,该列表中的每个项目都会被赋予一个数据上下文,该数据上下文是集合中的相应对象。在这种情况下,每个顶级 ListBoxItem 都绑定到一个 SurveyDetailViewModel 类型的对象。

      因此,您的 ItemsSource 只需:

      {Binding SurveyProductDetails}

      surveyDetail 可以省略。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 2012-11-21
        • 1970-01-01
        • 2022-10-04
        相关资源
        最近更新 更多