0

我需要在数据网格中显示一个图像,但它的显示如下

System.windows.controls.Image System.windows.controls.Image

我正在添加类型为图像的列的数据表,并且该行通过读取字节 [] 并转换为图像然后分配给数据表来构建。

//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

问题是什么?

4

2 回答 2

2

使用 dataGrid 的 AutoGeneratingColumn 事件来设置图像。

在 xaml 中定义数据模板

 <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

于 2012-12-26T08:40:33.650 回答
0

如果要在浏览器中显示图像,则必须生成一个 url 。您不能直接从二进制显示图像。

您可以尝试使用处理程序文件 (.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;
    }
    }
于 2012-09-13T07:24:34.063 回答