指定一个默认的空DataTemplate而不是默认的“ToString()”DataTemplate

14

在WPF应用程序中,默认的DataTemplate会显示.ToString()方法的结果。我正在开发一个应用程序,其中默认的DataTemplate不应该显示任何内容。

我已经尝试过:

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

但是这样做不起作用。有人知道是否可以在不为应用程序中的每个类类型指定特定的DataTemplate的情况下实现吗?

6个回答

9
如果您正在使用MVVM模式,并且有一个抽象类,所有的ViewModel类都派生自该类,则可以使用该类代替System.Object:
<Grid.Resources>
    <DataTemplate DataType="{x:Type vm:VMBase}">
    </DataTemplate>
</Grid.Resources>

2
你刚刚救了我的命。当然,这并不是字面意义上的,但这正是我正在寻找的。 - Firedragon

6

我不知道有什么方法可以做到这一点。根据下面Joe的评论,WPF明确禁止为类型Object指定DataTemplate

根据您的确切要求,可能更容易搜索匹配特定类型的DataTemplate。如果找到一个,就使用它。否则,不显示任何内容。例如:

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

在您的选择器中(伪代码,显然):
var dataTemplateKey = new DataTemplateKey() { DataType = theType; };
var dataTemplate = yourControl.FindResource(dataTemplateKey);

if (dataTemplate != null)
{
    return dataTemplate;
}

return NulloDataTemplate;

2
“WPF通过精确的运行时类型将对象与其DataTemplate匹配”--这是不正确的。如果您添加了一个DataType=BaseClass的DataTemplate,它也会匹配SubClass。我已经看到它工作过。不幸的是,框架明确禁止为System.Object创建DataTemplate;您会收到运行时错误“类型'DataTemplateKey'构造失败。DataTemplate.DataType不能是Object类型。” - Joe White
你说得对。我想的是样式,它们不会自动继承。我会更新我的回答。谢谢。 - Kent Boogaart

4

我使用了Nullable,这对我的情况有用。

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

1

这里有一个使用选择器完成此操作的工作示例(我认为这是最好的方法):

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
    }
}

1
我不确定是否要替换默认的DataTemplate,但是您可以使用ValueConverter,在某些类型的情况下传递显示ToString,并在其他情况下传递空字符串。以下是一些代码(请注意,typeb textblock上没有转换器,以展示其正常外观):
.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
    }
}

0
我无意中发现了一些东西。我正在使用自定义依赖属性在用户控件上设置Datacontext,该用户控件具有基于类型(在我的情况下是实体)的数据模板的ContentControl。由于我有几种不同类型的实体,所以我的自定义依赖属性是
` typeof(object)

这是我用来绑定到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)
          );

用法如下:

 MySelectedItem = SomeEntity;

我发现我也可以这样使用它:

 MySelectedItem = "some text";

而 contextcontrol 将打印一些文本作为它的上下文。

MySelectedItem = "";

适用于完全空白的上下文。

`


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接