Search code examples
wpfmvvmuser-controlsviewdatatemplate

Should I be using UserControls for my Views instead of DataTemplates?


I was reading this post and the author makes the suggestion that using DataTemplates to define a ViewModel is a lunatic's way to do it (#7). I do that all the time, is it really that bad?

<DataTemplate DataType="{x:Type local:MyViewModel}">
    <Grid>
        ...
    </Grid>
</DataTemplate>

Most of my Views are simply a ResourceDictionary that defines a DataTemplate or two. To me, it makes much better sense to do this than creating a UserControl for every ViewModel. Why would I want the extra layer in WPF's visual tree when it's not needed? And why would I want to take care of mapping ViewModels to Views when a DataTemplate does that for me? Is this syntax really a "lunatics approach"?


Solution

  • Nothing bad about it, except for incredibly large xaml files and the lack of edit support that DataTemplates have on the design surface.

    If those issues are hurting you, you can always...

    <DataTemplate DataType="{x:Type local:MyViewModel}">
        <local:MyViewModelUserControl />
    </DataTemplate>