locked
Issue with grouped list view RRS feed

  • Question

  • User372563 posted

    I am facing a wierd issue. I have a grouped list view it is working fine while loading the page, but when trying to load the same with same data on button click using a command, it's not showing up, anyone has any idea what is going on there

    Thursday, October 1, 2020 3:51 PM

Answers

  • User369979 posted

    You need to implement the INotifyPropertyChanged interface on the DataViewModel like: ``` public class DataViewModel : INotifyPropertyChanged { ObservableCollection groupedVeggieObject; public ObservableCollection GroupedVeggieObject { set { groupedVeggieObject = value; onpropertyChanged(); } get => groupedVeggieObject; }

    public ICommand Show { get; set; }
    public DataViewModel()
    {
        Show = new Command(ShowList);
    }
    
    private void ShowList()
    {
        GroupedVeggieObject = new ObservableCollection<GroupedVeggieObject>();
        var veggieGroup = new GroupedVeggieObject() { LongName = "vegetables", ShortName = "v" };
        var fruitGroup = new GroupedVeggieObject() { LongName = "fruit", ShortName = "f" };
        veggieGroup.Add(new VegModel() { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
        veggieGroup.Add(new VegModel() { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
        veggieGroup.Add(new VegModel() { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
        veggieGroup.Add(new VegModel() { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
        fruitGroup.Add(new VegModel() { Name = "banana", IsReallyAVeggie = false, Comment = "available in chip form factor" });
        fruitGroup.Add(new VegModel() { Name = "strawberry", IsReallyAVeggie = false, Comment = "spring plant" });
        fruitGroup.Add(new VegModel() { Name = "cherry", IsReallyAVeggie = false, Comment = "topper for icecream" });
        GroupedVeggieObject.Add(veggieGroup); GroupedVeggieObject.Add(fruitGroup);
    
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    void onpropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    } ``` So that the listview could respond to the value changed of GroupedVeggieObject. Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 5, 2020 2:20 AM

All replies

  • User369979 posted

    Did you use binding or set the items source directly? If you are resetting the source with binding, it could be caused by the property changed interface. Have you implemented the INotifyPropertyChanged on the view model? Could you please share some code here to help reproduce the issue?

    Friday, October 2, 2020 7:04 AM
  • User372563 posted

    We are setting the item source using the Bindings

    We were initializing the object only on button click. I have tried out a sample and you can find the sample application below which i have added to the github https://github.com/AlfinFelixA/SampleApp.git. Hope this helps

    Sunday, October 4, 2020 6:51 AM
  • User369979 posted

    You need to implement the INotifyPropertyChanged interface on the DataViewModel like: ``` public class DataViewModel : INotifyPropertyChanged { ObservableCollection groupedVeggieObject; public ObservableCollection GroupedVeggieObject { set { groupedVeggieObject = value; onpropertyChanged(); } get => groupedVeggieObject; }

    public ICommand Show { get; set; }
    public DataViewModel()
    {
        Show = new Command(ShowList);
    }
    
    private void ShowList()
    {
        GroupedVeggieObject = new ObservableCollection<GroupedVeggieObject>();
        var veggieGroup = new GroupedVeggieObject() { LongName = "vegetables", ShortName = "v" };
        var fruitGroup = new GroupedVeggieObject() { LongName = "fruit", ShortName = "f" };
        veggieGroup.Add(new VegModel() { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
        veggieGroup.Add(new VegModel() { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
        veggieGroup.Add(new VegModel() { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
        veggieGroup.Add(new VegModel() { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
        fruitGroup.Add(new VegModel() { Name = "banana", IsReallyAVeggie = false, Comment = "available in chip form factor" });
        fruitGroup.Add(new VegModel() { Name = "strawberry", IsReallyAVeggie = false, Comment = "spring plant" });
        fruitGroup.Add(new VegModel() { Name = "cherry", IsReallyAVeggie = false, Comment = "topper for icecream" });
        GroupedVeggieObject.Add(veggieGroup); GroupedVeggieObject.Add(fruitGroup);
    
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    void onpropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    } ``` So that the listview could respond to the value changed of GroupedVeggieObject. Here is the effect:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 5, 2020 2:20 AM
  • User372563 posted

    Thanks. it Worked

    Monday, October 5, 2020 6:31 AM
  • User369979 posted

    If it is helpful to you, could you please mark my reply as the answer?

    Monday, October 5, 2020 6:34 AM