1

我正在尝试将 ListView 与模型列表绑定。xaml 为 -

<ListView Name="lvProductBinding" HorizontalAlignment="Left" Height="434" Margin="10,144,0,0" VerticalAlignment="Top" Width="909">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Expander Header="{Binding ProductNo}" HorizontalAlignment="Left" Margin="967,153,-912,0" VerticalAlignment="Top" Width="895" Height="224" IsExpanded="False">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="195" VerticalAlignment="Top" Width="895" Margin="0,0,-2,0">
                        <Label  Content="{Binding ProductDescription}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" Height="27" />
                        <Label  Content="{Binding VendorName}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" Height="27" />
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在 mu xaml.cs 中,我在构造函数中执行此操作 -

List<ProductDetailsModel> products;
        products = new List<ProductDetailsModel>();

        ProductDetailsModel objProductDetailsModel = new ProductDetailsModel();
        objProductDetailsModel.VendorProductInventory = new VendorProductInventory() { ProductNo = "XS-3487", ProductDescription = "Perfume", VendorName = "JohnDoe" };
        products.Add(objProductDetailsModel);

        objProductDetailsModel = new ProductDetailsModel();
        objProductDetailsModel.VendorProductInventory = new VendorProductInventory() { ProductNo = "TT-23487", ProductDescription = "Shoes", VendorName = "Richard Gere" };
        products.Add(objProductDetailsModel);

        objProductDetailsModel = new ProductDetailsModel();
        objProductDetailsModel.VendorProductInventory = new VendorProductInventory() { ProductNo = "VFG-33487", ProductDescription = "Socks", VendorName = "Tom Cruise" };
        products.Add(objProductDetailsModel);

        lvProductBinding.ItemsSource = products;

ProductDetailsModel 类定义为 -

public class ProductDetailsModel : INotifyPropertyChanged
{
    public ProductDetailsModel()
    {

    }

    private VendorProductInventory _vendorProductInventory;
    public VendorProductInventory VendorProductInventory
    {
        get
        {
            return _vendorProductInventory;
        }
        set
        {
            if (_vendorProductInventory != value)
            {
                _vendorProductInventory = value;
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, e);
        }
    }


}

有人可以告诉我我在这里做错了什么。

急切地等待答复。谢谢,萨凯特

4

1 回答 1

0

您发布的代码有几处问题。

首先,正如 Sheridan 所指出的,您对 INotifyPropertyChanged 的​​实现是错误的,在 ListView 中的数据自动更新之前,它是正确的。

其次,XAML 文件中列出的所有绑定在 ProductDetailsModel 类中都没有匹配的公共属性。很可能您的 ListView 正在填充并且所有绑定都失败(您应该在运行时在 VS 输出窗口中看到此异常)。所写的绑定应如下所示:

{Binding Path=VendorProductInventory.ProductNo}
于 2013-11-14T20:26:31.740 回答