简体   繁体   中英

Binding class to ListView data template

I am trying to bind a ListView with a Model List. The xaml is as -

<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>

In mu xaml.cs I am doing this inside the constructor -

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;

where the ProductDetailsModel class is defined as -

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


}

Can someone please advise as to what I am doing wrong here.

Eagerly waiting for a reply. Thanks, Saket

There are several things wrong with the code you posted.

First, as Sheridan noted, your implementation of INotifyPropertyChanged is wrong, automatic updates to the data in the ListView won't work until it is correct.

Second, all of the bindings listed in your XAML file don't have matching public properties in the ProductDetailsModel class. More than likely your ListView is being populated and all the bindings are failing (you should see exceptions for this in the VS output window at runtime). Your bindings, as written, should look like:

{Binding Path=VendorProductInventory.ProductNo}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM