我需要在datagrid中显示一个图像,但它的显示如下
System.windows.controls.Image System.windows.controls.Image
数据表我正在添加类型为Image的列,并且通过读取bytes []并转换为图像然后分配给数据表来控制该行。
//Creating the column type
if (header.ColumnDescription == "ActiveStatus")
{
dc = new DataColumn(header.ColumnDescription, typeof(Image));
dt.Columns.Add(dc);
}
//Filling the data column
foreach (DataColumn col in dt.Columns)
{
dr[col] = GetRowItem(device, col.Caption);
}
dt.Rows.Add(dr);
//Logic for getting the image
Image img=new Image();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/Resources/Images/cloud_logon_radio_btn_green.png");
logo.EndInit();
img.Source = logo
问题是什么?
答案 0 :(得分:2)
使用dataGrid的AutoGeneratingColumn事件来设置图像。
在xaml中定义Datatemplate
<DataTemplate x:Key="ActivaStatusColumnTemplate">
<Image DataContext="{Binding}" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="red.png" />
<Style.Triggers>
<DataTrigger Value="On" Binding="{Binding}">
<Setter Property="Source" Value="green.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
//代码隐藏
private void dgView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column.Header.ToString() == "ActiveStatus")
{
// Create a new template column.
MyDataGridTemplateColumn templateColumn = new MyDataGridTemplateColumn();
templateColumn.CellTemplate = (DataTemplate)Resources["ActivaStatusColumnTemplate"];
templateColumn.ColumnName = e.PropertyName; // so it knows from which column to get binding data
e.Column = templateColumn;
e.Column.Header = "ActiveStatus";
}
}
//定义类以覆盖DataGridTemplateColumn
/// <summary>
/// Custom class derieved fromt the DataGridTemplateColumn to set the property name.
/// </summary>
public class MyDataGridTemplateColumn : DataGridTemplateColumn
{
public string ColumnName
{
get;
set;
}
protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
// The DataGridTemplateColumn uses ContentPresenter with your DataTemplate.
ContentPresenter cp = (ContentPresenter)base.GenerateElement(cell, dataItem);
// Reset the Binding to the specific column. The default binding is to the DataRowView.
BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding(this.ColumnName));
return cp;
}
}
指 http://social.msdn.microsoft.com/Forums/en/wpf/thread/8b2e94b7-3c44-4642-8acc-851de5285062
答案 1 :(得分:0)
如果要将图像显示到浏览器中,则必须生成网址。 你无法直接从二进制文件中显示图像。
您可以尝试使用处理程序文件(.ashx)将page_load的代码放入该文件中 并在您的处理程序代码,如
public void ProcessRequest (HttpContext context) {
//Get the picture id by url
//Query here
byte[] picture = queryoutput;
Response.ContentType = "images/jpeg";
Response.BinaryWrite(picture);
}
public bool IsReusable {
get {
return false;
}
}