2012-09-13 154 views
0

我需要在DataGrid中顯示的圖像,但它顯示像下面Datagrid的不具約束力的圖像

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

我是數據表添加類型爲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 

什麼問題?

回答

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; 
    } 
    } 
2

使用AutoGeneratingColumn事件您處理程序代碼。

定義的DataTemplate在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