【问题标题】:Is it possible to create MVVM properties using for/while loop? [closed]是否可以使用 for/while 循环创建 MVVM 属性? [关闭]
【发布时间】:2014-01-16 22:23:04
【问题描述】:

是否可以使用 for/while 循环创建 MVVM 属性?我应该在哪里放置循环?在构造函数中还是任何其他方法有效?我会使用其他一些属性或构造函数参数作为计数器。

我正在使用 WPF 4.0

是否可以创建包含ObservableCollection<string> {get: set;} 的列表或数组,即。列表或数组中的 string/int 属性 observable 集合。

喜欢

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

我正在尝试在不创建类的情况下执行以下操作。

public class CustomerListList : List<CustomerList> { }  

public class CustomerList : List<Customer> { }

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

我在“诗人”对以下问题的回答中找到了上面的代码。 Creating a List of Lists in C#

【问题讨论】:

  • 这不是列表/数组的用途吗?
  • @McGarnagle 你能告诉我怎么做吗?我尝试使用 for 循环,但无法使其工作。
  • 当然可以,但是您可以发布您现有的代码吗?我不确定我是否明白你在问什么。
  • @McGarnagle 是否可以创建包含 ObservableCollection {get: set;} 的列表或数组,即。列表或数组中的 string/int 属性 observable 集合。
  • I am trying to do something like below without creating a class.“有点像”在什么意义上?达到什么目的?为什么不能只创建一个类?

标签: c# wpf silverlight windows-phone-7 mvvm


【解决方案1】:

我不确定你在问什么。如果您的意思是可以创建字符串类型的 ObservableCollection 类型的列表,那么是的:

List<ObservableCollection<string>> list;

您可以通过 for 循环将项目添加到此列表中吗?当然:

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

【讨论】:

    【解决方案2】:

    嗯,您可以在视图模型中编写它的最通用方式是使用ObservableCollection&lt;ObservableCollection&lt;object&gt;&gt;。我指定了object,因为您希望在最终集合中使用ints 或strings。

    所以,在你的构造函数中创建初始列表,然后在你需要的地方简单地填充它。 ObservableCollection 的独特之处在于它会在发生变化时通知 UI。

    您可能遇到的唯一问题是,当您更改最里面的对象时,UI 中不会刷新任何内容,因为只有可观察的集合在更新时才会发布通知。真正解决这个问题的正确方法是使用以下结构:

    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));
        }
    }
    

    你在你的其他类中使用它是这样的:

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

    只要您想要或需要,只需将内容添加到该集合中即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2018-07-30
      • 2023-04-09
      • 2016-03-25
      • 2014-03-22
      • 2014-03-31
      相关资源
      最近更新 更多