![[PukiWiki] [PukiWiki]](image/pukiwiki.png) 
 SilverLight?
DataGridコントロール
this.dataGrid1.Columns.Add(new DataGridTextColumn { Header = "ID", Binding = new System.Windows.Data.Binding("id") });
■方法一
テンプレートの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);
スタイルの定義:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
   <Style x:Key="AlignCenter" TargetType="sdk:DataGridCell">
       <Setter Property="HorizontalContentAlignment" Value="Center" />
       <Setter Property="VerticalContentAlignment" Value="Center" />
   </Style>
</ResourceDictionary>
XAML側の使用:
<sdk:DataGrid AutoGenerateColumns="False"                       
     CellStyle="{Binding Source={StaticResource AlignCenter}}">
コメント:
0
  yVoC[UNLIMITȂ1~] ECirŃ|C Yahoo yV
 ECirŃ|C Yahoo yV LINEf[^[Ōz500~`I