SPREAD for WPF 3.0J - GcSpreadSheet
チェックボックス型セル
SPREAD for WPF 3.0J - GcSpreadSheet > 開発者ガイド > セル型 > チェックボックス型セル

チェックボックス型セルはセルにチェックボックスを表示します。CheckBoxCellType クラスを使用して設定します。主な設定は次のとおりです。

表示内容

チェックボックスにテキストを表示する場合、Content プロパティを使用します。また TrueContent プロパティでチェックされたときのテキストを、FalseContent プロパティでチェックが外されたときのテキストを、IndeterminateContent プロパティでチェック状態が未確定のときのテキストを設定できます。

3ステートのチェックボックスを表示するには、IsThreeState プロパティを「True」に設定します。

XAMLの使用

コードのコピー
<gss:GcSpreadSheet x:Name="GcSpreadSheet" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
    <gss:GcSpreadSheet.Sheets>
        <gss:SheetInfo ColumnCount="20" RowCount="50">
            <gss:SheetInfo.Columns>
                <gss:ColumnInfo Width="180">
                    <gss:ColumnInfo.CellType>
                        <CT:CheckBoxCellType IsThreeState="True" FalseContent="却下" TrueContent="承認" IndeterminateContent="保留"/>
                    </gss:ColumnInfo.CellType>
                </gss:ColumnInfo>
           </gss:SheetInfo.Columns>
        </gss:SheetInfo>
    </gss:GcSpreadSheet.Sheets>
</gss:GcSpreadSheet>                                        

コードの使用

コードのコピー
// CheckBoxCellTypeを設定します。
CheckBoxCellType cb = new CheckBoxCellType();
cb.IsThreeState = True;
cb.IndeterminateContent = "保留";
cb.FalseContent = "却下";
cb.TrueContent = "承認";
GcSpreadSheet.Workbook.ActiveSheet.Columns[2].CellType = cb;
コードのコピー
' CheckBoxCellTypeを設定します。
Dim cb As CheckBoxCellType = New CheckBoxCellType()
cb.IsThreeState = [True]
cb.IndeterminateContent = "保留"
cb.FalseContent = "却下"
cb.TrueContent = "承認"
GcSpreadSheet.Workbook.ActiveSheet.Columns(2).CellType = cb

次の図のようにチェックボックスにテキスト以外の内容を表示する場合、ContentTemplate プロパティにデータテンプレートを設定します。

次のサンプルコードはContentTemplateプロパティでデータテンプレートを設定する方法を示します。

コードのコピー
<gss:GcSpreadSheet Name="GcSpreadSheet">
    <gss:GcSpreadSheet.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>
    </gss:GcSpreadSheet.Resources>
    <gss:GcSpreadSheet.Sheets>
        <gss:SheetInfo ColumnCount="20" RowCount="50">
            <gss:SheetInfo.Columns>
                <gss:ColumnInfo Width="180">
                    <gss:ColumnInfo.CellType>
                        <CT:CheckBoxCellType IsThreeState="True" ContentTemplate="{StaticResource MyContent}"/>
                    </gss:ColumnInfo.CellType>
                </gss:ColumnInfo>
            </gss:SheetInfo.Columns>
        </gss:SheetInfo>
    </gss:GcSpreadSheet.Sheets>
</gss:GcSpreadSheet>                                        
メモ: CheckBoxCellType クラスの Content プロパティにコントロールなど WPF の UI 要素を設定することはできません。UI 要素を設定する場合は ContentTemplate プロパティを使用してください。

コマンド

ユーザーがチェックボックスを操作したときの処理は WPF のコマンドに実装します。実行時、コマンド パラメータには操作されたチェックボックスのセルの情報を保存した CellCommandParameter が自動的に設定されます。そのため、開発者は CellCommandParameter の情報を使用してコマンドを実装できます。CellCommandParameter に設定される情報は次のとおりです。

プロパティ 説明
Row クリックされるボタンの行を取得します。
Column クリックされるボタンの列を取得します。
CustomCommandParameter コマンドに渡すパラメーターを取得または設定します。。
WorkSheet クリックされるボタンのワークシートを取得します。

次のサンプルコードは、チェックボックスをチェックした行のインデックスをポップアップで表示する処理を MyCheckCommand クラスに実装します。

コードのコピー
CheckBoxCellType chk = new CheckBoxCellType();
chk.Content = "承認";
chk.Command = new MyCheckCommand(GcSpreadSheet);
GcSpreadSheet.Workbook.ActiveSheet.Columns[0].CellType = chk;
コードのコピー
Dim chk As CheckBoxCellType = New CheckBoxCellType()
chk.Content = "承認"
chk.Command = New MyCheckCommand(GcSpreadSheet)
GcSpreadSheet.Workbook.ActiveSheet.Columns(0).CellType = chk
コードのコピー
public class MyCheckCommand : System.Windows.Input.ICommand
{
    private GrapeCity.Wpf.SpreadSheet.GcSpreadSheet _gcSpreadSheet;
    public MyCheckCommand(GrapeCity.Wpf.SpreadSheet.GcSpreadSheet gcSpreadSheet)
    {
        this._gcSpreadSheet = gcSpreadSheet;
    }
    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)
    {
        if (parameter is CellCommandParameter cellCmdParam)
        {
            MessageBox.Show(string.Format("[{0}, {1}]セルです。", cellCmdParam.Row, cellCmdParam.Column));
        }
    }
}
コードのコピー
Public Class MyCheckCommand
    Implements System.Windows.Input.ICommand

    Private _gcSpreadSheet As GrapeCity.Wpf.SpreadSheet.GcSpreadSheet

    Public Sub New(ByVal gcSpreadSheet As GrapeCity.Wpf.SpreadSheet.GcSpreadSheet)
        Me._gcSpreadSheet = gcSpreadSheet
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean
        Return True
    End Function

    Public Event CanExecuteChanged As EventHandler

    Public Sub OnCanExecuteChanged()
        RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
    End Sub

    Public Sub Execute(ByVal parameter As Object)
        Dim cellCmdParam As CellCommandParameter = Nothing

        If CSharpImpl.__Assign(cellCmdParam, TryCast(parameter, CellCommandParameter)) IsNot Nothing Then
            MessageBox.Show(String.Format("[{0}, {1}]セルです。", cellCmdParam.Row, cellCmdParam.Column))
        End If
    End Sub
End Class