[]
        
(Showing Draft Content)

ボタン型セル

ボタン型セルはセルにボタンを表示します。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 の情報

プロパティ

説明

Area

ボタンが配置された領域

CellPosition

セルの位置

CustomCommandParameter

ボタン型セルの 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 = btn
Public 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

関連トピック

セル型

セル型の設定

セル型とセルの値

セル型と編集用コントロール

セル型とイベント

標準型セル

テキスト型セル

日付時刻型セル

数値型セル

マスク型セル

コンボボックス型セル

チェックボックス型セル

ラジオグループ型セル

イメージ型セル

データテンプレート型セル