[GcSpreadGrid] ボタン型セルを非活性または非表示にする方法

文書番号 : 85088     文書種別 : 使用方法     登録日 : 2020/05/21     最終更新日 : 2020/05/21
文書を印刷する
対象製品
SPREAD for WPF 3.0J
詳細
SPREADに組み込まれている既定のボタン型セルには、ボタンの活性/非活性や表示/非表示を切り替える機能は備えられておりません。この場合は、IsEnabledプロパティ、およびIsVisiableプロパティを追加した独自のButtonCellTypeを用意してセルに適用します。

次のようなコードで実装可能です。

◎サンプルコード(VB)
Imports GrapeCity.Windows.SpreadGrid

Class MainWindow
    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        Dim btn As ButtonCellType = New ButtonCellType
        btn.Content = "ボタン"
        btn.Command = New MyCommand
        gcSpreadGrid1.Columns(0).CellType = btn

        Dim mybtn1 As MyButtonCellType = New MyButtonCellType
        mybtn1.Content = "ボタン"
        mybtn1.Command = New MyCommand
        mybtn1.IsEnabled = False
        mybtn1.IsVisiable = True
        gcSpreadGrid1(0, 0).CellType = mybtn1

        Dim mybtn2 As MyButtonCellType = New MyButtonCellType
        mybtn2.Content = "ボタン"
        mybtn2.Command = New MyCommand
        mybtn2.IsEnabled = True
        mybtn2.IsVisiable = False
        gcSpreadGrid1(2, 0).CellType = mybtn2

    End Sub
End Class

Public Class MyCommand
    Implements ICommand

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

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

    Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
        MessageBox.Show("ボタンが押下されました。")
    End Sub
End Class

Public Class MyButtonCellType
    Inherits ButtonCellType

    Private _buttons As List(Of Button) = New List(Of Button)

    Private _isEnabled As Boolean
    Public Property IsEnabled As Boolean
        Get
            Return Me._isEnabled
        End Get
        Set
            Me._isEnabled = Value
            If (Me._buttons.Count > 0) Then
                For Each button In Me._buttons
                    button.IsEnabled = Value
                Next
            End If

        End Set
    End Property

    Private _isVisiable As Boolean
    Public Property IsVisiable As Boolean
        Get
            Return Me._isVisiable
        End Get
        Set
            Me._isVisiable = Value
            If (Me._buttons.Count > 0) Then
                For Each button In Me._buttons
                    button.Visibility = If(Value, Visibility.Visible, Visibility.Hidden)
                Next
            End If

        End Set
    End Property

    Protected Overrides Function GenerateDisplayElement() As FrameworkElement
        Dim button = CType(MyBase.GenerateDisplayElement, Button)
        button.IsEnabled = Me.IsEnabled
        button.Visibility = If(Me.IsVisiable, Visibility.Visible, Visibility.Hidden)
        Me._buttons.Add(button)
        Return button
    End Function
End Class
◎サンプルコード(C#)
using GrapeCity.Windows.SpreadGrid;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ButtonCellType btn = new ButtonCellType();
        btn.Content = "ボタン";
        btn.Command = new MyCommand();
        gcSpreadGrid1.Columns[0].CellType = btn;

        MyButtonCellType mybtn1 = new MyButtonCellType();
        mybtn1.Content = "ボタン";
        mybtn1.Command = new MyCommand();
        mybtn1.IsEnabled = false;
        mybtn1.IsVisiable = true;
        gcSpreadGrid1[0, 0].CellType = mybtn1;

        MyButtonCellType mybtn2 = new MyButtonCellType();
        mybtn2.Content = "ボタン";
        mybtn2.Command = new MyCommand();
        mybtn2.IsEnabled = true;
        mybtn2.IsVisiable = false;
        gcSpreadGrid1[2, 0].CellType = mybtn2;
    }
}

public class MyCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        MessageBox.Show("ボタンが押下されました。");
    }
}

public class MyButtonCellType : ButtonCellType
{
    private List<Button> _buttons = new List<Button>();
    private bool _isEnabled;
    public bool IsEnabled
    {
        get
        {
            return this._isEnabled;
        }
        set
        {
            this._isEnabled = value;
            if (this._buttons.Count > 0)
            {
                foreach (var button in this._buttons)
                {
                    button.IsEnabled = value;
                }
            }
        }
    }

    private bool _isVisiable = true;
    public bool IsVisiable
    {
        get
        {
            return this._isVisiable;
        }
        set
        {
            this._isVisiable = value;
            if (this._buttons.Count > 0)
            {
                foreach (var button in this._buttons)
                {
                    button.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                }
            }
        }
    }

    protected override FrameworkElement GenerateDisplayElement()
    {
        var button = base.GenerateDisplayElement() as Button;
        button.IsEnabled = this.IsEnabled;
        button.Visibility = this.IsVisiable ? Visibility.Visible : Visibility.Hidden;
        this._buttons.Add(button);
        return button;
    }
}