我发现在客户端使用AutoGeneratingColumn事件句柄(调用我的WCF服务的Silverlight应用程序)也适用。不一定是我的问题的答案,但我认为这是有用的知道。我会添加评论,但代码太长。
private void dgResults_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
if (e.PropertyType == typeof(byte[])) {
e.Column.Header = e.Column.Header + "_D";
// Create a new template column.
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = e.Column.Header + "_E";
templateColumn.CellTemplate = (DataTemplate)Resources["imgTemplate"];
templateColumn.CellEditingTemplate = (DataTemplate)Resources["imgTemplate"];
// ...
// Replace the auto-generated column with the templateColumn.
e.Column = templateColumn;
}
}
参考资料[“imgTemplate”在Silverlight中.XAML文件被创建和验证码是其代码隐藏。
<UserControl.Resources>
<local:BinaryArrayToURIConverter x:Key="binaryArrayToURIConverter" />
<DataTemplate x:Key="imgTemplate">
<Image x:Name="img" Source="{Binding GraphicBytes,Converter={StaticResource binaryArrayToURIConverter}}"/>
</DataTemplate>
</UserControl.Resources>
地方:指主XAML声明的一部分:
xmlns:local="clr-namespace:<your namespace here>"
为BinaryArrayToURIConverter代码:
public class BinaryArrayToURIConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
MemoryStream ms = new MemoryStream((byte[])value);
BitmapImage image = new BitmapImage();
image.SetSource(ms);
return image;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
的
可能重复[在WCF我怎么返回一个类,包含一个System.Drawing.Image属性?](http://stackoverflow.com/questions/1767864/in-wcf-how-do-i-return-a-class-that-contains-a-system-drawing-image -property) – Yuck
@Yuck - 与此类似。实际上我之前读过这篇文章,目前正在使用与转换为字节数组和将其标记为DataMember相关的部分答案。但是,我试图找到一种方法来不必创建字节数组。 – David
从这个问题的接受答案:*“无论如何,你需要把它变成一个字节[]或流,以通过电线,并补充它作为一个图像。”* – Yuck