[]
ボタン型セルはセルにボタンを表示します。ButtonCellType クラスを使用して設定します。主な設定は次のとおりです。
!type=note
メモ:デザイナを使用した設定方法については、「ボタン型セルの設定」をご覧ください。
次の図のようにボタンにテキストを表示する場合、Content プロパティを使用します。

サンプルコード
ButtonCellType btn = new ButtonCellType();
btn.Content = "ボタン";
gcSpreadGrid1.Columns[0].CellType = btn;Dim btn As New ButtonCellType()
btn.Content = "ボタン"
GcSpreadGrid1.Columns(0).CellType = btn次の図のようにボタンにテキスト以外の内容を表示する場合、ContentTemplate プロパティにデータ テンプレートを設定します。

サンプルコード
<sg:GcSpreadGrid DefaultRowHeight="30">
<sg:GcSpreadGrid.Resources>
<DataTemplate x:Key="MyContent">
<Path Stretch="Uniform" Fill="#FF000000" Data="F1 M 173.354,624.292L 173.354,594.078L 188.461,609.185M 177.738,583.325C 163.457,583.325 151.88,594.902 151.88,609.185C 151.88,623.468 163.457,635.044 177.738,635.044C 192.021,635.044 203.598,623.468 203.598,609.185C 203.598,594.902 192.021,583.325 177.738,583.325 Z "/>
</DataTemplate>
</sg:GcSpreadGrid.Resources>
<sg:GcSpreadGrid.Columns>
<sg:Column Width="30">
<sg:Column.CellType>
<sg:ButtonCellType ContentTemplate="{StaticResource MyContent}"/>
</sg:Column.CellType>
</sg:Column>
</sg:GcSpreadGrid.Columns>
</sg:GcSpreadGrid>!type=note
メモ:ButtonCellType クラスの Content プロパティにコントロールなど WPF の UI 要素を設定することはできません。UI 要素を設定する場合は ContentTemplate プロパティを使用してください。
ユーザーがボタンを押したときの処理は WPF のコマンドに実装します。実行時、コマンド パラメータにはボタンが押されたセルの情報を保存した CellCommandParameter が自動的に設定されます。そのため、開発者は CellCommandParameter の情報を使用してコマンドを実装できます。CellCommandParameter に設定される情報は次のとおりです。
CommandParameter の情報
プロパティ | 説明 |
|---|---|
ボタンが配置された領域 | |
セルの位置 | |
ボタン型セルの CustomCommandParameter プロパティが設定されている場合、その内容 |
次のサンプルコードは、ボタンが押された行を削除する処理を MyDeleteCommand クラスに実装します。
サンプルコード
ButtonCellType btn = new ButtonCellType();
btn.Content = "削除";
btn.Command = new MyDeleteCommand(gcSpreadGrid1);
gcSpreadGrid1.Columns[0].CellType = btn;public class MyDeleteCommand : ICommand
{
private GcSpreadGrid _gcSpreadGrid;
public MyDeleteCommand(GcSpreadGrid gcSpreadGrid)
{
this._gcSpreadGrid = gcSpreadGrid;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void OnCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
public void Execute(object parameter)
{
CellCommandParameter cellCommandParameter = (CellCommandParameter)parameter;
if (cellCommandParameter.Area == SpreadArea.Cells)
{
this._gcSpreadGrid.Rows.Remove(cellCommandParameter.CellPosition.Row);
}
}
}Dim btn As New ButtonCellType()
btn.Content = "削除"
btn.Command = New MyDeleteCommand(gcSpreadGrid1)
GcSpreadGrid1.Columns(0).CellType = btnPublic Class MyDeleteCommand
Implements ICommand
Private _gcSpreadGrid As GcSpreadGrid
Public Sub New(gcSpreadGrid As GcSpreadGrid)
Me._gcSpreadGrid = gcSpreadGrid
End Sub
Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
Return True
End Function
Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
Public Sub OnCanExecuteChanged()
RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
End Sub
Public Sub Execute(parameter As Object) Implements ICommand.Execute
Dim cellCommandParameter As CellCommandParameter = DirectCast(parameter, CellCommandParameter)
If cellCommandParameter.Area = SpreadArea.Cells Then
Me._gcSpreadGrid.Rows.Remove(cellCommandParameter.CellPosition.Row)
End If
End Sub
End Class