Silverlight显示数据库二进制字段图片(RadTileView)
DB
[Photo] [varbinary](max)
ImageConvert.cs
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Media.Imaging; using System.IO; using System.Windows.Data; namespace SGSGHome.Views.Common { public class ImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return null; } byte[] imgByte = (byte[])value; BitmapImage img = new BitmapImage(); img.SetSource(new MemoryStream(imgByte)); return img; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
Xaml
<controls:ChildWindow x:Class="SGSGHome.Views.Common.UserProfileView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" Width="1200" Height="600" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:local="clr-namespace:SGSGHome.Views.Common" Title="人员档案"> <ContentControl.Resources> <local:ImageConverter x:Key="imgConvert"/> <DataTemplate x:Key="contentTemplate"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="70"/> <RowDefinition Height="30"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Image Source="{Binding Photo,Converter={StaticResource imgConvert}}" Grid.RowSpan="2" HorizontalAlignment="Right"/> <Grid Grid.RowSpan="2" Grid.Column="1"> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="25"/> <RowDefinition Height="25"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="80"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Text="名称:" FontWeight="Bold" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"/> <TextBlock Text="{Binding SamAccountName}" TextDecorations="Underline" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/> <TextBlock Text="职位:" FontWeight="Bold" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"/> <TextBlock Text="{Binding Title}" TextDecorations="Underline" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/> <TextBlock Text="邮箱:" FontWeight="Bold" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"/> <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/> <TextBlock Text="电话:" FontWeight="Bold" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"/> <TextBlock Text="{Binding Ext}" TextDecorations="Underline" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/> </Grid> <StackPanel Grid.Row="2" Grid.ColumnSpan="2" Margin="5,20,5,5"> <Border Background="#DDFFFFFF" BorderThickness="5" BorderBrush="SkyBlue" CornerRadius="10" Padding="5"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="30"/> <RowDefinition Height="90"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="60"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Text="负责项目:" FontWeight="Bold" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding ProjectRelateDesc}" TextDecorations="Underline" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="项目经理:" FontWeight="Bold" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding ProjectManagerDesc}" TextDecorations="Underline" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="移动手机:" FontWeight="Bold" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding Mobile}" Grid.Row="2" TextDecorations="Underline" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="Q Q号码:" FontWeight="Bold" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding QQ}" Grid.Row="3" TextDecorations="Underline" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="所属部门:" FontWeight="Bold" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding Department}" TextDecorations="Underline" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="部门经理:" FontWeight="Bold" Grid.Row="5" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding Manager}" TextDecorations="Underline" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="公司名称:" FontWeight="Bold" Grid.Row="6" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding Company}" TextDecorations="Underline" Grid.Row="6" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBlock Text="个人说明:" FontWeight="Bold" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top"/> <TextBlock Text="{Binding SelfDesc}" TextDecorations="Underline" Grid.Row="7" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap"/> </Grid> </Border> </StackPanel> </Grid> </DataTemplate> <DataTemplate x:Key="headerTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding DisplayName}" /> </StackPanel> </DataTemplate> </ContentControl.Resources> <Grid x:Name="LayoutRoot" Margin="0"> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition Height="*" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> <TextBlock Margin="1" VerticalAlignment="Center">架构组织:</TextBlock> <ComboBox Name="cbLevelTag" Margin="3" Width="100" Height="23" ></ComboBox> <TextBlock Margin="1" VerticalAlignment="Center">负责项目:</TextBlock> <ComboBox Name="cbProjectRelate" Margin="3" Width="200" Height="23" ></ComboBox> <TextBlock Margin="1" VerticalAlignment="Center">项目经理:</TextBlock> <ComboBox Name="cbProjectManager" Margin="3" Width="200" Height="23" ></ComboBox> <Button Name="btnSearch" Content="查询" Width="40" Height="23" Click="btnSearch_Click" Margin="1" ></Button> <Button Name="btnReset" Content="清空" Width="40" Height="23" Click="btnReset_Click" Margin="1"></Button> </StackPanel> <telerik:RadTileView telerik:StyleManager.Theme="Metro" Grid.Row="1" Name="tv" ItemsSource="{Binding}" MinimizedColumnWidth="300" ContentTemplate="{StaticResource contentTemplate}" ItemTemplate="{StaticResource headerTemplate}" /> <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="2" /> <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="2" /> </Grid> </controls:ChildWindow>
Binding
tv.DataContext = e.Result;