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,221
  • 14
  • 43
  • 67
  • Isn't that what a list/array is for? – McGarnagle Jan 16 '14 at 22:26
  • @McGarnagle Can you show how to do that? I tried using a for loop but couldn't make it work. – user2330678 Jan 16 '14 at 22:27
  • 5
    Sure, but can you post your existing code? I'm not sure I even understand what you're asking. – McGarnagle Jan 16 '14 at 22:32
  • @McGarnagle Is it possible to create Lists or Arrays containing ObservableCollection {get: set;} ie. string/int property observable collection in a list or array. – user2330678 Jan 16 '14 at 23:23
  • Oy ... sorry, but I'm not understanding the point. It might help if you described what you're trying to do/what this is for. – McGarnagle Jan 16 '14 at 23:39
  • 1
    `I am trying to do something like below without creating a class.` "Something like" in what sense? To what purpose? And why can't you just create a class? – McGarnagle Jan 16 '14 at 23:46

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,565
  • 1
  • 15
  • 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
  • 7,014
  • 1
  • 25
  • 34