I have an app that needs to display an input form for each property of a class. The thing is, I don't know which class I'm dealing with at the compile-time, so it needs to be dynamic. Now, I used to do this before in code-behind like:
foreach (var P in TheType.GetProperties()){
this.ControlStackPanel.Children.Add(/.../)
// and so on
}
But this time I'm trying to do this in pure MVVM pattern, so I cannot use code-behind. My idea was to inject an instance of my window to the ViewModel through constructor, but I've been told that it completely ruins the MVVM pattern.
So, any ideas on how this could be done?
PropertyGrid. I don't understand the problem though. Why it has to bedynamic? ViewModel should not know anything about view. ViewModel can prepare a list of something what will be used by view to generate children though. Pure MVVM - move code into behaviors/converters/etc, but please don't move it into ViewModel.ObservableCollection<Item>, whereItemholds all necessary properties (Icon,Text, whatever you will need). The view can check itsDataContextinLoadedto get ViewModel instance. Then you can bind that collection toItemsControl.ItemsSourceand using data templates or template selectors visualize those (in data template you can define binding to concrete property of either ViewModels). Or if you don't mind non-pure MVVM (I don't), do it in the View as you already do, but using that collection.