ElementWrapper, care to elaborate a little ?

Topics: Common questions
Mar 30, 2013 at 10:04 PM
I have been using some of your ideas in my own WPF applicatino and Windows 8 store apps as i have found it intuitive to use.

But this question i havent been able to answer my self.

I get the idea of wrapping Views back to Model and your example for Focus is fine.
But why is the element protected in ElementWrapper, meaning that whenever someone want to do this on something else then focus he have to extend your code?

Would it not make sense to have the wrapper just expose the view element directly to be used in the model ?
Mar 30, 2013 at 11:59 PM
This will break the whole idea of mvvm. A model should not know about its view.
But a wrapper is in an intermediate layer that can work without a view and can be replaced with something else in e.g. unit tests.
Mar 31, 2013 at 12:49 AM

I have a case here where i had a button that when clicked it should open a new tab in a tabcontrol based on the item selected in a listview.
First i tried to use a command, EventCommand and CommandParameter resulting in the button being set to disabled because canExecute returned false the first time when no items was selected in the list. Instead i tried binding just a command with a RelayCommand, but then i had to have a wrapper for the listview to get the selected item in my model.

So making the Elements public on the Wrapper was a lot easier then writing these intermediate layers, but i do see your argument.

I havent been able to figure out why you have a Set of Elements in the wrappers? In what situation can there be more than one element in a wrapper? My guess is that you can bind two elements to the same wrapper, but why would one do that?
Mar 31, 2013 at 1:52 AM
Elements is protected on purpose so you cannot break the rules.

Do not use wrappers where you can use bindings. Selected items should be bound.

Multiple elements come from multiple bindings.
Mar 31, 2013 at 2:00 AM
I do want to databind, but could not make it work with selected items. I have a button and when it is clicked a new viewmodel is added to my TabItemsViewModelsCollection which will add a new tab to my TabControl. My Problem was to get the Command and CommandParameter to work on the button.

I had something like <button CommandParameter="{Binding SelectedItem ,ElementName=MyListView}" Command="{Binding OpenTabCommand}"> ...

While reproducing my error it hit me that the problem was i had bound it to SelectedItem isntead of Selecteditems, which made the eventcommand CanExecute return false and not being set to true. Changing it to SelectedItems and handing Count>0 in my Command delegate solved my problems.

Thanks for the discussion, helped me a lot.