DataGridコントロール

 動的にDataGridの組み立て方法を紹介します。

一般の列を利用する場合

this.dataGrid1.Columns.Add(new DataGridTextColumn { Header = "ID", Binding = new System.Windows.Data.Binding("id") });

テンプレート列(DataGridTemplateColumn)を利用する場合

■方法一

テンプレートのXAMLのコードを定義する

private string CreateColumnTemplate( string propertyName)
{
   StringBuilder CellTemp = new StringBuilder();
   CellTemp.Append("<DataTemplate ");
   CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/"); CellTemp.Append("2006/xaml/presentation' ");
   CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>"); 
   CellTemp.Append(String.Format("<TextBlock Text='{{Binding Path={0}}}'/>", propertyName)); 
   CellTemp.Append("</DataTemplate>");
   return CellTemp.ToString();
}

C#側のソースコード:
※System.Windows.Markup.XamlReaderクラスを利用して、Xamlを読み込む

DataGridTemplateColumn dtc = new DataGridTemplateColumn();
dtc.CellTemplate = (DataTemplate)XamlReader.Load(CreateColumnTemplate( "id"));
this.dataGrid2.Columns.Add(dtc);

■方法二

リソースにテンプレートを定義します。

<Application.Resources>
   <ResourceDictionary>
        <DataTemplate x:Key="myCellTemplate">
            <TextBlock Text="{Binding Path=id}"/>
        </DataTemplate>
   </ResourceDictionary>                
</Application.Resources>

C#側のソースコード:

DataGridTemplateColumn dtc = new DataGridTemplateColumn();
dtc.CellTemplate = (DataTemplate)App.Current.Resources["myCellTemplate"];           
this.dataGrid2.Columns.Add(dtc);


ウィジェット型CM動画配信サービス manna [マナ]

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結解除 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:41:36 (1627d)

PCpዾyǗlgĂ܂}WŔ܂z 萔O~ył񂫁z Yahoo yV NTT-X Store

z[y[W ̃NWbgJ[h COiq ӂ邳Ɣ[ COsیI COze