Ben varsayılan DataTemplate yerine hakkında emin değilim ama ekran ToString geçirmek için bir ValueConverter kullanabilirsiniz Aksi halde belirli türlerde ve boş bir dizede.
.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
}
}
"WPF tam zamanı türüne göre kendi DataTemplate sahip bir nesne eşleşir". DataType = BaseClass ile bir DataTemplate eklerseniz, bu da Alt Sınıf ile eşleşecektir. Onu gördüm. Ne yazık ki, çerçeve özellikle System.Object için bir DataTemplate yapma izin vermez; Bir çalışma zamanı hatası "tip 'DataTemplateKey' inşaat başarısız oldu. DataTemplate.DataType tipi nesne olamaz." olsun –
Haklısınız. Otomatik olarak devralınmayan Stilleri düşünüyordum. Cevabımı güncelleme. Teşekkürler. –