我会使用一个基于对象类型DataTrigger
设置的转换器,并使用一个返回的转换器来获取类型Image.Source
typeof(value)
<Style x:Key="MyStyle" TargetType="{x:Type Image}">
<!-- Default Value -->
<Setter Property="Source" Value="{StaticResource CompanyIcon}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}"
Value="{x:Type local:Employee}">
<Setter Property="Source" Value="{StaticResource EmployeeIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}"
Value="{x:Type local:Customer}">
<Setter Property="Source" Value="{StaticResource CustomerIcon}" />
</DataTrigger>
</Style.Triggers>
</Style>
我通常使用的转换器如下所示:
public class ObjectToTypeConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
return value.GetType();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}