Specify a default empty DataTemplate instead of the default 'ToString()' DataTemplate Specify a default empty DataTemplate instead of the default 'ToString()' DataTemplate wpf wpf

Specify a default empty DataTemplate instead of the default 'ToString()' DataTemplate


If you are using the MVVM pattern and have an abstract class which all your ViewModel classes derive from, you can use that class instead of System.Object:

<Grid.Resources>    <DataTemplate DataType="{x:Type vm:VMBase}">    </DataTemplate></Grid.Resources>


I know of no way to do this. As per Joe's comment below, WPF specifically disallows specifying a DataTemplate for type Object.

Depending on your exact requirements, it may be easier to search for a DataTemplate that matches the specific type. If you find one, use it. Otherwise, display nothing. For example:

<ContentControl Content="{Binding YourContent}" ContentTemplateSelector="{StaticResource MyContentTemplateSelector}"/>

And in your selector (pseudo-code, obviously):

var dataTemplateKey = new DataTemplateKey() { DataType = theType; };var dataTemplate = yourControl.FindResource(dataTemplateKey);if (dataTemplate != null){    return dataTemplate;}return NulloDataTemplate;


I used Nullable, worked for my situation.

<DataTemplate DataType="{x:Type sys:Nullable}"><!-- Content --></DataTemplate>