14

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

我试过了:

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

但这不起作用。如果不为应用程序中的每个类类型指定特定的 DataTemplate,有谁知道这是否可行?

4

6 回答 6

8

如果您使用 MVVM 模式并且有一个所有 ViewModel 类都派生自的抽象类,则可以使用该类而不是 System.Object:

<Grid.Resources>
    <DataTemplate DataType="{x:Type vm:VMBase}">
    </DataTemplate>
</Grid.Resources>
于 2011-03-14T04:21:52.750 回答
6

我知道没有办法做到这一点。根据 Joe 在下面的评论,WPF 明确禁止DataTemplate为 type指定 a Object

根据您的确切要求,搜索DataTemplate与特定类型匹配的 a 可能会更容易。如果你找到一个,使用它。否则,什么也不显示。例如:

<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;
于 2009-04-02T16:24:06.973 回答
4

我使用了 Nullable,适合我的情况。

<DataTemplate DataType="{x:Type sys:Nullable}">
<!-- Content -->
</DataTemplate>
于 2015-07-23T12:43:44.390 回答
1

我不确定是否要替换默认的 DataTemplate,但您可以使用 ValueConverter 在某些类型的情况下传递显示 ToString,否则可以使用空字符串。这是一些代码(请注意,typeb 文本块上没有转换器来显示它正常的样子):

.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
    }
}
于 2009-04-03T23:56:59.670 回答
0

这是一个关于如何使用选择器(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
    }
}
于 2017-08-19T16:11:59.170 回答
0

我偶然发现了一些东西。我正在使用自定义依赖属性在用户控件上设置 Datacontext,该用户控件具有基于类型(在我的情况下为实体)的 Datatemplates 的内容控件。由于我有几种不同类型的实体,我的自定义依赖属性是

` 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";

上下文控件将打印一些文本作为其上下文。

MySelectedItem = "";

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

`

于 2018-03-29T13:33:40.757 回答