나는 기본 DataTemplate을 교체에 대한 확실하지 않다,하지만 당신은 디스플레이 ToString을 전달하는 ValueConverter을 사용할 수 있습니다 특정 유형의 경우에는 그렇지 않으면 빈 문자열입니다.
.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는 정확한 실행시의 형태로 그 DataTemplate을 가진 개체를 일치". DataType = BaseClass 인 DataTemplate을 추가하면 SubClass와도 일치합니다. 나는 그것이 작동하는 것을 보았다. 안타깝게도이 프레임 워크에서는 System.Object 용 DataTemplate을 만들지 않습니다. 런타임 오류가 발생합니다. "형식 'DataTemplateKey'이 (가) 생성되지 못했습니다 .DataTemplate.DataType은 Object를 입력 할 수 없습니다." –
당신 말이 맞아요. Styles는 자동으로 상속받지 않습니다. 내 대답 업데이트 중. 감사. –