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 は特にDataTemplatefor typeの指定を禁止していObjectます。

正確な要件によっては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;
于 2009-04-02T16:24:06.973 に答える
4

私はNullableを使用し、私の状況で働いていました。

<DataTemplate DataType="{x:Type sys:Nullable}">
<!-- Content -->
</DataTemplate>
于 2015-07-23T12:43:44.390 に答える
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
    }
}
于 2009-04-03T23:56:59.670 に答える