我發現在客戶端使用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