作成日: 2013/06/21 最終更新日: 2013/06/21
文書種別
使用方法
詳細
1. デザイン時にセルの色を変更する方法
デザイン時にセルの色を変更するには、DataGridCellPresenterのスタイルを作成してBackground/Foregroundプロパティを設定し、列のCellStyleプロパティを作成したスタイルに設定します。
◎サンプルコード(XAML)
2. 実行時にセルの色を変更する方法
実行時にセルの色を変更するには、C1DataGrid.LoadedCellPresenterイベントでセルPresenterのBackground/Foregroundプロパティを設定します。また、C1DataGrid.UnloadedCellPresenterイベントでセルPresenterのBackground/Foregroundプロパティをnullに設定して初期化します。
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
デザイン時にセルの色を変更するには、DataGridCellPresenterのスタイルを作成してBackground/Foregroundプロパティを設定し、列のCellStyleプロパティを作成したスタイルに設定します。
◎サンプルコード(XAML)
<UserControl.Resources>
<Style x:Key="ColumnStyle" TargetType="c1:DataGridCellPresenter">
<Setter Property="Background" Value="DarkGray" />
<Setter Property="Foreground" Value="White" />
</Style>
</UserControl.Resources>
<c1:C1DataGrid Name="c1DataGrid1" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Column1" Binding="{Binding Column1, Mode=OneWay}" CellStyle="{StaticResource ColumnStyle}" />
<c1:DataGridTextColumn Header="Column2" Binding="{Binding Column2, Mode=TwoWay}" />
<c1:DataGridTextColumn Header="Column3" Binding="{Binding Column3, Mode=TwoWay}" />
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
<Style x:Key="ColumnStyle" TargetType="c1:DataGridCellPresenter">
<Setter Property="Background" Value="DarkGray" />
<Setter Property="Foreground" Value="White" />
</Style>
</UserControl.Resources>
<c1:C1DataGrid Name="c1DataGrid1" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Column1" Binding="{Binding Column1, Mode=OneWay}" CellStyle="{StaticResource ColumnStyle}" />
<c1:DataGridTextColumn Header="Column2" Binding="{Binding Column2, Mode=TwoWay}" />
<c1:DataGridTextColumn Header="Column3" Binding="{Binding Column3, Mode=TwoWay}" />
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
2. 実行時にセルの色を変更する方法
実行時にセルの色を変更するには、C1DataGrid.LoadedCellPresenterイベントでセルPresenterのBackground/Foregroundプロパティを設定します。また、C1DataGrid.UnloadedCellPresenterイベントでセルPresenterのBackground/Foregroundプロパティをnullに設定して初期化します。
◎サンプルコード(Visual Basic)
Imports C1.WPF.DataGrid
Private Sub C1DataGrid1_LoadedCellPresenter(sender As System.Object, e As DataGridCellEventArgs)
If e.Cell.Row.Index = 0 AndAlso e.Cell.Column.Index = 0 Then
e.Cell.Presenter.Background = New SolidColorBrush(Colors.Red)
e.Cell.Presenter.Foreground = New SolidColorBrush(Colors.White)
End If
If e.Cell.Row.Index = 1 Then
e.Cell.Presenter.Background = New SolidColorBrush(Colors.Blue)
e.Cell.Presenter.Foreground = New SolidColorBrush(Colors.White)
End If
End Sub
Private Sub C1DataGrid1_UnloadedCellPresenter(sender As System.Object, e As DataGridCellEventArgs)
' Presenterのアンロード時に色を初期化します。
e.Cell.Presenter.Background = Nothing
e.Cell.Presenter.Foreground = Nothing
End Sub
Private Sub C1DataGrid1_LoadedCellPresenter(sender As System.Object, e As DataGridCellEventArgs)
If e.Cell.Row.Index = 0 AndAlso e.Cell.Column.Index = 0 Then
e.Cell.Presenter.Background = New SolidColorBrush(Colors.Red)
e.Cell.Presenter.Foreground = New SolidColorBrush(Colors.White)
End If
If e.Cell.Row.Index = 1 Then
e.Cell.Presenter.Background = New SolidColorBrush(Colors.Blue)
e.Cell.Presenter.Foreground = New SolidColorBrush(Colors.White)
End If
End Sub
Private Sub C1DataGrid1_UnloadedCellPresenter(sender As System.Object, e As DataGridCellEventArgs)
' Presenterのアンロード時に色を初期化します。
e.Cell.Presenter.Background = Nothing
e.Cell.Presenter.Foreground = Nothing
End Sub
◎サンプルコード(C#)
using C1.WPF.DataGrid;
private void c1DataGrid1_LoadedCellPresenter(object sender, DataGridCellEventArgs e)
{
if (e.Cell.Row.Index == 0 && e.Cell.Column.Index == 0)
{
e.Cell.Presenter.Background = new SolidColorBrush(Colors.Red);
e.Cell.Presenter.Foreground = new SolidColorBrush(Colors.White);
}
if (e.Cell.Row.Index == 1)
{
e.Cell.Presenter.Background = new SolidColorBrush(Colors.Blue);
e.Cell.Presenter.Foreground = new SolidColorBrush(Colors.White);
}
}
private void c1DataGrid1_UnloadedCellPresenter(object sender, DataGridCellEventArgs e)
{
// Presenterのアンロード時に色を初期化します。
e.Cell.Presenter.Background = null;
e.Cell.Presenter.Foreground = null;
}
private void c1DataGrid1_LoadedCellPresenter(object sender, DataGridCellEventArgs e)
{
if (e.Cell.Row.Index == 0 && e.Cell.Column.Index == 0)
{
e.Cell.Presenter.Background = new SolidColorBrush(Colors.Red);
e.Cell.Presenter.Foreground = new SolidColorBrush(Colors.White);
}
if (e.Cell.Row.Index == 1)
{
e.Cell.Presenter.Background = new SolidColorBrush(Colors.Blue);
e.Cell.Presenter.Foreground = new SolidColorBrush(Colors.White);
}
}
private void c1DataGrid1_UnloadedCellPresenter(object sender, DataGridCellEventArgs e)
{
// Presenterのアンロード時に色を初期化します。
e.Cell.Presenter.Background = null;
e.Cell.Presenter.Foreground = null;
}
旧文書番号
80165