Guest User

Untitled

a guest
May 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public class TypeSwitchConverter : Dictionary<Type, object>, IValueConverter
  2. {
  3. public object Convert(object value, Type targetType, object parameter, .CultureInfo culture)
  4. {
  5. foreach (var mapping in this)
  6. {
  7. if (mapping.Key.IsAssignableFrom(value.GetType()))
  8. {
  9. return mapping.Value;
  10. }
  11. }
  12.  
  13. return null;
  14. }
  15.  
  16. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  17. {
  18. throw new NotImplementedException();
  19. }
  20. }
  21.  
  22. <ListBox ItemsSource="{Binding}">
  23. <ListBox.ItemTemplate>
  24. <DataTemplate>
  25. <TextBlock Text="{Binding}">
  26. <TextBlock.Style>
  27. <Binding>
  28. <Binding.Converter>
  29. <my:TypeSwitchConverter>
  30. <Style x:Key="{x:Type cor:Int32}" TargetType="{x:Type TextBlock}">
  31. <Setter Property="Background" Value="Red" />
  32. </Style>
  33. <Style x:Key="{x:Type cor:String}" TargetType="{x:Type TextBlock}">
  34. <Setter Property="Background" Value="Green" />
  35. </Style>
  36. <Style x:Key="{x:Type sys:Uri}" TargetType="{x:Type TextBlock}">
  37. <Setter Property="Background" Value="Blue" />
  38. </Style>
  39. </my:TypeSwitchConverter>
  40. </Binding.Converter>
  41. </Binding>
  42. </TextBlock.Style>
  43. </TextBlock>
  44. </DataTemplate>
  45. </ListBox.ItemTemplate>
  46. </ListBox>
Add Comment
Please, Sign In to add comment