Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TypeSwitchConverter : Dictionary<Type, object>, IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, .CultureInfo culture)
- {
- foreach (var mapping in this)
- {
- if (mapping.Key.IsAssignableFrom(value.GetType()))
- {
- return mapping.Value;
- }
- }
- return null;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- <ListBox ItemsSource="{Binding}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding}">
- <TextBlock.Style>
- <Binding>
- <Binding.Converter>
- <my:TypeSwitchConverter>
- <Style x:Key="{x:Type cor:Int32}" TargetType="{x:Type TextBlock}">
- <Setter Property="Background" Value="Red" />
- </Style>
- <Style x:Key="{x:Type cor:String}" TargetType="{x:Type TextBlock}">
- <Setter Property="Background" Value="Green" />
- </Style>
- <Style x:Key="{x:Type sys:Uri}" TargetType="{x:Type TextBlock}">
- <Setter Property="Background" Value="Blue" />
- </Style>
- </my:TypeSwitchConverter>
- </Binding.Converter>
- </Binding>
- </TextBlock.Style>
- </TextBlock>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
Add Comment
Please, Sign In to add comment