<UserControl x:Class="GoogleAuthDemo.ObjectBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GoogleAuthDemo"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<UserControl.Resources>
<local:ObjectPropertiesConverter x:Key="ObjectPropertiesConvert"/>
<DataTemplate x:Key="PropertyTemplate">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Key}" Margin="0,0,10,1"/>
<TextBlock Text="{Binding Value}" TextWrapping="Wrap" TextAlignment="Right" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ScrollViewer>
<ItemsControl ItemTemplate="{StaticResource PropertyTemplate}"
ItemsSource="{Binding Path=., Converter={StaticResource ObjectPropertiesConvert}}">
</ItemsControl>
</ScrollViewer>
</UserControl>
With a smidge of C# to create a name value pair collection, given an object's properties
public class ObjectPropertiesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
return from p in value.GetType().GetProperties()
where p.CanRead
select new KeyValuePair<string, string>
(
p.Name,
p.GetValue(value, null) != null ? p.GetValue(value, null).ToString() : null
);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
CodeProject
No comments:
Post a Comment