Kunal Chawla
Kunal Chawla

Reputation: 55

Listview Not binding through ViewModel

I am getting a problem while I am binding the ListView with a ViewModel. I have created an object of ViewModel class and set it as BindingContext of ListView. This object contains 4 items when it gets initialised but the ListView remains blank. I am not sure what I am missing here. I have added the code below -

public class LeadershipViewModel
{
    private PfsServiceArea _oldProduct;
    public ObservableCollection Products { get; set; }
    public LeadershipViewModel()
    {
        Products = new ObservableCollection();
        var PFSArea = new PfsServices().GetPFSServiceArea("Leadership");
        if (PFSArea.Count > 0){
            foreach(PfsServiceArea pf in PFSArea){
            Products.Add(pf);
            }
        }
    }

    public void ShowOrHidePoducts(PfsServiceArea product)
    {
        if (_oldProduct == product)
        {
            // click twice on the same item will hide it
            product.ShowDescription = false;
            product.ShowDisplay = true;
            UpdateProducts(product);
        }
        else
        {
            if (_oldProduct != null)
            {
                // hide previous selected item
                product.ShowDescription = true;
                product.ShowDisplay = false;
                UpdateProducts(_oldProduct);
            }
            // show selected item
            product.ShowDescription = true;
            product.ShowDisplay = false;
            UpdateProducts(product);
        }

        _oldProduct = product;
    }

    private void UpdateProducts(PfsServiceArea product)
    {
        var index = Products.IndexOf(product);
        Products.Remove(product);
        Products.Insert(index, product);
    }

And I have binded it with ListView like below -

Private LeadershipViewModel leader = new LeadershipViewModel();
sicCodeList = new CustomListview(ListViewCachingStrategy.RetainElement)
{
    HorizontalOptions = LayoutOptions.StartAndExpand,
    VerticalOptions = LayoutOptions.EndAndExpand,
    ItemTemplate = sicDataTemplate,
    SeparatorVisibility = SeparatorVisibility.None,
    Margin = new Thickness(-5, 0, 0, 0),
    BindingContext = leader
};
sicCodeList.HasUnevenRows = true;
//sicCodeList.BindingContext = leader;
sicCodeList.SetBinding(ListView.ItemsSourceProperty, "Products");
sicCodeList.ItemTapped += (sender, e) => OnItemTapped(sender, e);

Upvotes: 1

Views: 46

Answers (2)

sme
sme

Reputation: 4153

Its better to set the BindingContext of the page to your ViewModel. Then, you can bind the list to your listview's ItemsSource:

private LeadershipViewModel leader = new LeadershipViewModel();

public MyPage()
{
    InitializeComponent();
    BindingContext = leader;
    ListView sicCodeList = new ListView() { ... set properties ... };
    sicCodeList.SetBinding(ListView.ItemsSourceProperty, "Products");
}

Upvotes: 1

Fahadsk
Fahadsk

Reputation: 1109

I think you forgot to set ItemSource property for your ListView

sicCodeList.ItemSource = leader.Products;

more info here

Upvotes: 1

Related Questions