Binding to "object.GetType ()"?
I have
ObservableCollection<object> Let's consider that we have 2 elements:
int a = 1; string str = "hey!"; My xaml file is attached to it via a DataContext, and I would like to display the Type (System.Type) of the object with a binding. Here is the code I have
<TextBlock Text="{Binding}"/> And I would like to display in my TextBlocks:
int string Thanks for any help!
For this you will need an IValueConverter .
[ValueConversion(typeof(object), typeof(string))] public class ObjectToTypeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value == null ? null : value.GetType().Name // or FullName, or whatever } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new InvalidOperationException(); } } Then add it to your resources ...
<Window.Resources> <my:ObjectToTypeConverter x:Key="typeConverter" /> </Window.Resources> Then use it to snap
<TextBlock Text="{Binding Mode=OneWay, Converter={StaticResource typeConverter}}" />