Why is CompositeCollection not Freezable?

前端 未结 1 696
悲哀的现实
悲哀的现实 2020-12-03 13:39

I am writing an application using the MVVM pattern. I am providing data to my view by setting my view\'s DataContext property to an instance of my ViewModel. Generally I j

1条回答
  •  -上瘾入骨i
    2020-12-03 14:17

    I just tried this tonight:

    public class State
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }
    
    public class MyWindowViewModel
    {
        ObservableCollection _states = new ObservableCollection
        {
            new State { Code = "FL", Name = "Florida" },
            new State { Code = "CA", Name = "California" },
        };
    
        public ObservableCollection States
        {
            get
            {
                return _states;
            }
        }
    }
    
    
    
      
        
      
    
      
        
          
            
              
              
              
            
          
        
      
    
    

    The key here is to create an instance of your service locator as a static resource then go through it to get to your viewmodel. The service locator can wire up to instances of the ViewModel using Unity or whatever DI you want.

    Edit:

    Actually in my silverlight app I create the service locator as a static resoure in the App.xaml and then bind my other UserControls/Windows/Pages DataContext to a ViewModel property of the service locator. It should still work the same way for the combo boxes though even if the service locator is instantiated in the App.xaml's resources. I wish there was a silverlight version of CompositeCollection that I could use. This would work great for the app I'm working on. :(

    0 讨论(0)
提交回复
热议问题