1

Is it possible to create MVVM properties using for/while loop? Where should I place the loop? In constructor or will any other method work? I would be using some other property or constructor parameter as a counter.

I am using WPF 4.0

Is it possible to create Lists or Arrays containing ObservableCollection<string> {get: set;} ie. string/int property observable collection in a list or array.

like

List<ObservableCollection<string> {get;set;} lt= new List<ObservableCollection<string> {get;set;}();

I am trying to do something like below without creating a class.

public class CustomerListList : List<CustomerList> { }  

public class CustomerList : List<Customer> { }

public class Customer
{
   public int ID { get; set; }
   public string SomethingWithText { get; set; }
}

I found above code in "The Poet"'s answer to below question. Creating a List of Lists in C#

Community
  • 1
  • 1
user2330678
  • 2,081
  • 13
  • 42
  • 65

2 Answers2

1

I'm not sure what you're asking. If you mean is it possible to create a List of type ObservableCollection of type string, then yes:

List<ObservableCollection<string>> list;

Can you add items to this list via a for loop? Sure:

for (var i = 0; i < 99; i++) {
    list.Add(new ObservableCollection<string> { "1", "2", "hi" });
}
Neil Smith
  • 2,505
  • 1
  • 13
  • 18
0

Well, the most generic way you could write that in your view model is by using ObservableCollection<ObservableCollection<object>>. I specified object because you want either ints or strings in your final collection.

So, create the initial list in your constructor and simply fill it wherever you need it. The unique property of ObservableCollection is that it will notify the UI when something changes.

The only problem you might run into is that when you change innermost objects, nothing will refresh in the UI since only observable collections post notifications when updated. The proper way to actually solve this is using the following structure:

public class Customer : INotifyPropertyChanged
{
    private int _id;
    private string _somethingWithText;

    public int ID
    {
        get { return _id;}
        set
        {
            _id = value;
            OnPropertyChanged();
        }
    }

    public string SomethingWithText
    {
        get { return _somethingWithText; }
        set
        {
            _somethingWithText = value;
            OnPropertyChanged();
        }
    }

    public PropertyChangedEventHandler PropertyChanged;
    protected OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

And you use it in your other class like this:

public class MainViewModel
{
    public ObservableCollection<ObservableCollection<Customer>> Customers { get; set; }

    public MainViewModel()
    {
        Customers = new ObservableCollection<ObservableCollection<Customer>>();
    }
}

Whenever you want or need, simply add stuff to that collection.

Toni Petrina
  • 6,854
  • 1
  • 24
  • 34