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#

6
  • Isn't that what a list/array is for? Commented Jan 16, 2014 at 22:26
  • @McGarnagle Can you show how to do that? I tried using a for loop but couldn't make it work. Commented Jan 16, 2014 at 22:27
  • 5
    Sure, but can you post your existing code? I'm not sure I even understand what you're asking. Commented Jan 16, 2014 at 22:32
  • @McGarnagle Is it possible to create Lists or Arrays containing ObservableCollection<string> {get: set;} ie. string/int property observable collection in a list or array. Commented Jan 16, 2014 at 23:23
  • 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? Commented Jan 16, 2014 at 23:46

2 Answers 2

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" });
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.