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}}">
DataGridのDetailsTemplateにネストのDataGridを入れます。
<sdk:RowDetailsTemplate>
<DataTemplate>
<sdk:DataGrid ItemsSource="{Binding YourChildCollectionPropertyName}" />
</DataTemplate>
</sdk:RowDetailsTemplate>
Windows Form のようなdataGrid1.Rows[0].Cells[1]みたいなプロパティが提供されていませんので、LoadingRowイベントを利用して、Rowがロードする時、セルの値を取得する
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
//5番目列のセルを取得する
Image image = (Image)dataGrid1.Columns[5].GetCellContent(e.Row);
//ここの「Thumbnail」はデータバインド時、データの列名
Byte[] bits = ((Comics)e.Row.DataContext).Thumbnail;
if (image != null && bits != null && bits.Length > 0)
{
BitmapImage bi = new BitmapImage();
bi.SetSource(new MemoryStream(bits));
image.Source = bi;
}
}
DataGridに画像とかのオブジェクトを入れると、カラムのヘッダーをクリックしても、ソートができなくなります。
対策:
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn SortMemberPath="バインドデータ中のソースしたいプロパティを定義する">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
DataGridView内行の背景を変更するのは、下記のような処理で反映できない。
(e.Row as DataGridRow).BackGround = new SolidColorBrush()
下記のようなスタイル定義の方法で、行背景を変更できます。
(e.Row as DataGridRow).Style = Application.Current.Resources["DataGridRowStyle"] as Style;
<Style x:Key="DataGridRowStyle" TargetType="sdk:DataGridRow">
<Setter Property="Background" Value="#F0FF0000"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridRow">
<sdk:DataGridFrozenGrid x:Name="Root" >
<sdk:DataGridFrozenGrid.Resources>
<Storyboard x:Key="DetailsVisibleTransition">
<DoubleAnimation Duration="00:00:0.1" Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight"/>
</Storyboard>
</sdk:DataGridFrozenGrid.Resources>
<sdk:DataGridFrozenGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</sdk:DataGridFrozenGrid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="NormalAlternatingRow">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To=".5"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NormalSelected">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOverSelected">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="UnfocusedSelected">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To="1"/>
<ColorAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" To="#FFE1E7EC"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="Invalid">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<sdk:DataGridFrozenGrid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</sdk:DataGridFrozenGrid.RowDefinitions >
<Border BorderBrush="Black" BorderThickness="2" Grid.ColumnSpan="2" Grid.RowSpan="2" CornerRadius="4"></Border>
<Rectangle x:Name="BackgroundRectangle" Fill="#FFBADDE9" Opacity="0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<Rectangle x:Name="InvalidVisualElement" Fill="#FFF7D8DB" Opacity="0" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<sdk:DataGridRowHeader x:Name="RowHeader" Grid.RowSpan="3" sdk:DataGridFrozenGrid.IsFrozen="True"/>
<sdk:DataGridCellsPresenter x:Name="CellsPresenter" Grid.Column="1" sdk:DataGridFrozenGrid.IsFrozen="True"/>
<sdk:DataGridDetailsPresenter x:Name="DetailsPresenter" Grid.Column="1" Grid.Row="1"/>
<Rectangle x:Name="BottomGridLine" Height="1" HorizontalAlignment="Stretch" Grid.Column="1" Grid.Row="2"/>
</sdk:DataGridFrozenGrid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
参照情報:
http://msdn.microsoft.com/ja-jp/library/cc278066(vs.95).aspx
コメント:
0