This is based on @AndyG's answer but is a bit safer because it's strongly typed.
Implement an IValueConverter named DataTypeConverter, which accepts an object and returns its Type (as a System.Type):
public class DataTypeConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
return value?.GetType() ?? Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
Change your DataTrigger to use the Converter, and set the value to the Type:
<DataTrigger Binding="{Binding SelectedItem,
Converter={StaticResource DataTypeConverter}}"
Value="{x:Type local:MyType}">
...
</DataTrigger>
Declare DataTypeConverter in the resources:
<UserControl.Resources>
<v:DataTypeConverter x:Key="DataTypeConverter"></v:DataTypeConverter>
</UserControl.Resources>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…