WPF, UserControl or DataTemplate

Zach picture Zach · Nov 27, 2009 · Viewed 12.9k times · Source

Recently I'm trying to reuse some UI elements in my application. When I started programming with WPF I'm told that DataTemplate is the best way to reuse UI elements. You can define a template for your data entity and use it everywhere. It sounds very good. However, I also found some disadvantages, especially when it is compared with UserControl.

  1. You cannot reuse a DataTemplate defined in another Window or UserControl. For example, if UserDataTemplate is define in WindowA.xaml, you cannot use it in WindowB.xaml. The solution might be that put the DataTemplate as a resource in a global Resource Dictionary.
  2. DataTemplate is hard to have some code behind. As mentioned in item 1, if you put the DataTemplate in a ResourceDictionary, there is no place to put your code by default. I googled the problem and yes, I found a trick to make the ResourceDictionary have a cs file. But it still has another problem.
  3. Another problem of DataTemplate is that you must be clear with the difference between the instance of DataTemplate itself and the instances of the content of DataTemplate. A DataTemplate will have only one "instance of DataTemplate", and may have many instances of the content of the DataTemplate. Let me explain it with an Example:

    <DataTemplate>
            <DataTemplate.Resources>
                    <my:User x:key="User1"/>
            </DataTemplate.Resources>                
            <Grid MouseLeftButtonDown="OnMouseLeftButtonDown">
                    <Grid.Resources>
                            <my:User x:key="User2"/>
                    </Grid.Resources>
            </Grid>        
    </DataTemplate>
    
    
    public partial class CodeBehind
    {
             Point mousePos = new Point();
    
            private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
            {
                    mousePos = e.Pos...;
            }
    }
    

The result will be that : User1 will only have one instance, however, a User2 instance will be created once the DataTemplate is applied, which means User2 will have many instances if the datatemplate is being applied many times. However, unlike UserControl, the field "mousePos" will NOT have many copies. If the DataTemplate is being applied 100 times, the mousePos won't have 100 copies, which mean the 100 Grids will use the only one mousePos field as the same time, which may cause problems. In UserControl, the field you defined will only be used by the control. 100 UserControl instances will have 100 field copies.

Maybe I'm using DataTemplate in the wrong way. Any comment is appreciated.

Best regards,

Zach

Answer

Brad Cunningham picture Brad Cunningham · Nov 27, 2009

Conceptually DataTemplates and UserControls solve two different problems. They are not really interchangeable so your comparisons are not really accurate.

DataTemplates are all about applying a visual style to a DataType. Typically what this means is I have my own .NET class called Foo and I want to give it a visual style. I would do this by creating a DataTemplate with a DataType of Foo.

I can then place this DataTemplate in my application (say in App.XAML) and I will have my visual style applied to my data object Foo wherever it is used. Often this means you will see a ContentControl that has a Content property bound to a property of type Foo.

UserControls on the other had are all about organization of XAML. A user control helps in organizing chunks of XAML that you want to re-use throughout your application that has behaviors and functionality tied to it. This is more than what a DataTempate will do.

A DataTemplate is tied to one DataType and display a visual for that type. A UserControl can be composed of multiple DataTypes and can include custom behaviors.

That being said, I very rarely find the need for a UserControl. I use DataTemplates all over to model my data and implement my behaviors though data bindings and the MVVM pattern.