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

6

14

The default DataTemplate in a wpf application displays the result of the .ToString() method. I'm developing an application where the default DataTemplate should display nothing.

I've tried:

<Grid.Resources>
  <DataTemplate DataType="{x:Type System:Object}">
   <Grid></Grid>
  </DataTemplate>
</Grid.Resources>

But this doesn't work. Does anyone knows if this is possible without specifiing a specific DataTemplate for every class type in the application?

Suggestible answered 2/4, 2009 at 14:3 Comment(0)
L
6

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;
Livi answered 2/4, 2009 at 16:24 Comment(2)
"WPF matches an object with its DataTemplate by exact runtime type" -- Not true. If you add a DataTemplate with DataType=BaseClass, it will match SubClass as well. I've seen it work. Unfortunately, the framework specifically disallows making a DataTemplate for System.Object; you get a runtime error "The type 'DataTemplateKey' failed construction. DataTemplate.DataType cannot be type Object."Indene
You're right. I was thinking of Styles, which aren't automatically inherited. Updating my answer. Thanks.Livi
J
9

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>
Justis answered 14/3, 2011 at 4:21 Comment(1)
You just saved my life. Well not literally of course but this is exactly what i was looking forThyme
L
6

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;
Livi answered 2/4, 2009 at 16:24 Comment(2)
"WPF matches an object with its DataTemplate by exact runtime type" -- Not true. If you add a DataTemplate with DataType=BaseClass, it will match SubClass as well. I've seen it work. Unfortunately, the framework specifically disallows making a DataTemplate for System.Object; you get a runtime error "The type 'DataTemplateKey' failed construction. DataTemplate.DataType cannot be type Object."Indene
You're right. I was thinking of Styles, which aren't automatically inherited. Updating my answer. Thanks.Livi
C
4

I used Nullable, worked for my situation.

<DataTemplate DataType="{x:Type sys:Nullable}">
<!-- Content -->
</DataTemplate>
Concussion answered 23/7, 2015 at 12:43 Comment(0)
T
1

I'm not sure about replacing the default DataTemplate, but you can use a ValueConverter to pass display ToString in the case of certain types and an empty string otherwise. Here's some code (note that the typeb textblock doesnt have the converter on it to show what it looks like normally):

.xaml:

<Window x:Class="EmptyTemplate.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:EmptyTemplate"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <loc:AType x:Key="atype"/>
        <loc:BType x:Key="btype"/>
        <loc:TypeConverter x:Key="TypeConverter"/>
    </Window.Resources>
    <StackPanel>
        <Button Content="{Binding Source={StaticResource atype}, Converter={StaticResource TypeConverter}}"/>
        <Button Content="{Binding Source={StaticResource btype}, Converter={StaticResource TypeConverter}}"/>
        <TextBlock Text="{Binding Source={StaticResource atype}, Converter={StaticResource TypeConverter}}"/>
        <TextBlock Text="{Binding Source={StaticResource btype}}"/>
    </StackPanel>
</Window>

.xaml.cs:

namespace EmptyTemplate
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class AType { }

    public class BType { }

    public class TypeConverter : IValueConverter
    {
        public DataTemplate DefaultTemplate { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType() == typeof(AType))
            {
                return value.ToString();
            }
            return DefaultTemplate;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
Trojan answered 3/4, 2009 at 23:56 Comment(0)
T
1

Here a working example about how to do this using a selector (the best way IMO):

public class EmptyDefaultDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item != null)
        {
            var dataTemplateKey = new DataTemplateKey(item.GetType());
            var dataTemplate = ((FrameworkElement) container).TryFindResource(dataTemplateKey);
            if (dataTemplate != null)
                return (DataTemplate) dataTemplate;
        }

        return new DataTemplate(); //null does not work
    }
}
Tuxedo answered 19/8, 2017 at 16:11 Comment(0)
C
0

I discovered something accidentally. I was using a custom dependency property to set the Datacontext on a usercontrol that had a contentcontrol with Datatemplates based on types(entities in my case). Since I had several different kinds of entities my custom dependency property was

` typeof(object)

This was the device I used to bind to the datacontext of the ContentControl.

 public object MySelectedItem
    {
        get { return (object)GetValue(Property1Property); }
        set { SetValue(Property1Property, value); }
    }

            public static readonly DependencyProperty Property1Property
        = DependencyProperty.Register(
              "MySelectedItem",
              typeof(object),
              typeof(PromotionsMenu),
              new PropertyMetadata(false)
          );

Used like this:

 MySelectedItem = SomeEntity;

I discovered I could also use it like this:

 MySelectedItem = "some text";

And the contextcontrol would print some text as its context.

MySelectedItem = "";

works for a totally blank context.

`

Cloudy answered 29/3, 2018 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.