2017-01-04 12 views
0

このコードを使用して、イメージとテキストを表示する列を作成していますが、テキストのみが表示されています。ImageおよびText WPFを使用してDataGridTemplateColumnを作成する

DataGridTemplateColumn col1 = new DataGridTemplateColumn(); col1.Header = "MyHeader";

  FrameworkElementFactory factoryStackPanel = new FrameworkElementFactory(typeof(System.Windows.Controls.StackPanel)); 
      factoryStackPanel.SetValue(System.Windows.Controls.StackPanel.OrientationProperty, Orientation.Vertical); 

      FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock)); 
      Binding bindTextBlock = new Binding("[" + i + "]"); 
      factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.TextProperty, bindTextBlock); 
      factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.TextWrappingProperty, TextWrapping.Wrap); 
      factoryTextBlock.SetValue(System.Windows.Controls.TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center); 

      FrameworkElementFactory factoryImage = new FrameworkElementFactory(typeof(System.Windows.Controls.Image)); 
      Binding bindImage = new Binding("http://www.pgn.co.id/images/modules/logo_pgn.png"); 
      factoryImage.SetValue(System.Windows.Controls.Image.SourceProperty, bindImage); 

      factoryStackPanel.AppendChild(factoryImage); 
      factoryStackPanel.AppendChild(factoryTextBlock); 

      DataTemplate cellTemplate = new DataTemplate() { VisualTree = factoryStackPanel }; 

      col1.CellTemplate = cellTemplate; 

      gridViewItens.Columns.Add(col1); 
+1

それは、XAMLでこのようなものを行うにははるかに簡単です。 –

答えて

0

Ed Plunkettのように、XAMLではこれを行うのがはるかにクリーンです。 DataGridTemplateColumnは同じように動作データテンプレートを使用しています

<DataGridTemplateColumn Header="My Item"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Image Source="{Binding MyImage}" /> 
       <TextBlock Text="{Binding MyText}" /> 
      </StackPanel> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
0

:あなたのDataGridは、いくつかの項目のObservableCollectionにバインドされ、そしてあなたアイテムクラスがMyTextMyImageのような性質を持っているように、あなたが何かを行うことができると仮定すると、データテンプレート の機能をリストコントロールで先に調べました。 DataGridTemplateColumnの唯一の違いは、 という2つのテンプレート(データ表示用(CellTemplate)とデータ編集用(CellEditingTemplate))の2つのテンプレートを定義できることです。ここではグリッド内の各製品のサムネイル画像を配置するテンプレートデータ列 を使用する例です:

<DataGridTemplateColumn> 
<DataGridTemplateColumn.CellTemplate> 
<DataTemplate> 
<Image Stretch="None" Source= 
"{Binding Path=ProductImagePath, Converter={StaticResource ImagePathConverter}}"> 
</Image> 
</DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
関連する問題