starsiv35
starsiv35

Reputation: 3

Use Model Multiple Times In Different Model MVVM

I have a model made who has uses a Child Model 2x as one of it's properties. I can get all of that data loaded correctly into the model but I can't figure out how to Display it into a datagrid Properly. Using test rows I know it's binding, but I can't get it to display any properties.

Model

class BidLine
    {
        public TestProduct CompetitorItem;
        public TestProduct BEItem;


        public ObservableCollection<BidLine> LoadProduct()
        {

            var tList = new ObservableCollection<BidLine>();

            tList.Add(new Model.BidLine
            {
                BEItem = new TestProduct
                {
                    ProductID = "test1",
                    VendorID = "vnd1"
                },

                CompetitorItem = new TestProduct
                {
                    ProductID = "test2",
                    VendorID = "vnd2"
                }
            });

            tList.Add(new Model.BidLine
            {
                BEItem = new TestProduct
                {
                    ProductID = "test1",
                    VendorID = "vnd1"
                },

                CompetitorItem = new TestProduct
                {
                    ProductID = "test2",
                    VendorID = "vnd2"
                }
            });

            return tList;
        }

    }

Child Model

class TestProduct : ModelBase
{
    private string _productid;
    public string ProductID
    {
        get { return _productid; }
        set
        {
            _productid = value;
            RaisePropertyChanged("ProductID");
        }
    }

    private string _vendorID;
    public string VendorID
    {
        get { return _vendorID; }
        set
        {
            _vendorID = value;
            RaisePropertyChanged("VendorID");
        }
    }
}

View Model

class ProductViewModel
{
    public ObservableCollection<BidLine> Products { get; set; }

    public ProductViewModel()
    {
        var dl = new BidLine();
        Products = dl.LoadProduct();
    }

Edit:

XAML Code

  <Grid>
<DataGrid ItemsSource="{Binding Path=Products}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=BEItem.ProductID}" Header="ProductID"/>
    </DataGrid.Columns>
</DataGrid>

Upvotes: 0

Views: 39

Answers (1)

AQuirky
AQuirky

Reputation: 5236

Binding does not work with fields. You need to have properties. So convert BEItem and CompetitorItem into properties...

    public TestProduct CompetitorItem { get; set; }
    public TestProduct BEItem { get; set; }

Things will start working. The best way to debug the binding is to watch the output window. If you had looked at the output window you would have seen this...

System.Windows.Data Error: 40 : BindingExpression path error: 'BEItem' property not found on 'object' ''BidLine' (HashCode=31093287)'

This clearly indicates that the binding engine is looking for a property BEItem when all that is present is a BEItem field.

Upvotes: 1

Related Questions