FlexGrid for WPF
エディタをカスタマイズする
基本操作 > 編集機能 > エディタをカスタマイズする

行や列単位でエディタをカスタマイズする場合は、C1FlexGridCellFactory クラスを使用します。CellFactory クラスはC1FlexGrid のデフォルトのセルファクトリを実装するクラスで、ICellFactory インターフェイスを実装するクラスを作成してそのクラスのインスタンスを CellFactory プロパティに割り当ててカスタムエディタを作成することを可能とします。

次のコードでは、グリッドは Product.cs と連結していることを前提とし、CellFactory クラスをオーバーライドしてC1NumericBox のカスタムエディタを列に埋め込む方法を示します。

【実行例】

 

コードのコピー
' カスタムエディタを作成します
Public Partial Class CustomEditor
    Inherits Window
    Public Sub New()
        InitializeComponent()
        C1FlexGrid.ItemsSource = Product.GetProducts(10)
        C1FlexGrid.CellFactory = New MyCellFactory()
    End Sub
    Public Class MyCellFactory
        Inherits CellFactory
        Public Overrides Function CreateCellEditor(grid As C1FlexGrid, cellType As CellType, rng As CellRange) As FrameworkElement
            Dim control = MyBase.CreateCellEditor(grid, cellType, rng)
            Dim bdr = TryCast(control, Border)
            If bdr IsNot Nothing Then
                ' カスタムエディタを1列目の2行目に割り当てます
                If rng.Row = 2 AndAlso rng.Column = 1 Then
                    Dim textBox = TryCast(bdr.Child, TextBox)
                    textBox.Background = Brushes.Yellow
                End If
                ' 2行2列目のセルではC1NumericBoxをカスタムエディタに割り当てます
                If rng.Column = 2 Then
                    Dim source = grid.Rows(rng.Row).DataItem
                    Dim path = grid.Columns(rng.Column).Binding.Path
                    Dim myBinding As New Binding() With { _
                        .Path = path, _
                        .Source = source, _
                        .Mode = BindingMode.TwoWay _
                    }
                    Dim numericBox As New C1NumericBox()
                    numericBox.SetBinding(C1NumericBox.ValueProperty, myBinding)
                    bdr.Child = Nothing
                    bdr.Child = numericBox
                End If
            End If
            Return control
        End Function
    End Class
End Class
コードのコピー
// カスタムエディタを作成します
public partial class CustomEditor : Window
{
    public CustomEditor()
    {
        InitializeComponent();
        C1FlexGrid.ItemsSource = Product.GetProducts(10);
        C1FlexGrid.CellFactory = new MyCellFactory();
    }
    public class MyCellFactory : CellFactory
    {
        public override FrameworkElement CreateCellEditor(C1FlexGrid grid, CellType cellType, CellRange rng)
        {
            var control = base.CreateCellEditor(grid, cellType, rng);
            var bdr = control as Border;
            if (bdr != null)
            {
                // カスタムエディタを1列目の2行目に割り当てます
                if (rng.Row == 2 && rng.Column == 1)
                {
                    var textBox = bdr.Child as TextBox;
                    textBox.Background = Brushes.Yellow;
                }
                // 2行2列目のセルではC1NumericBoxをカスタムエディタに割り当てます
                if (rng.Column == 2)
                {
                    var source = grid.Rows[rng.Row].DataItem;
                    var path = grid.Columns[rng.Column].Binding.Path;
                    Binding myBinding = new Binding() { Path = path, Source = source, Mode = BindingMode.TwoWay };
                    C1NumericBox numericBox = new C1NumericBox();
                    numericBox.SetBinding(C1NumericBox.ValueProperty, myBinding);

                    bdr.Child = null;
                    bdr.Child = numericBox;
                }
            }
            return control;
        }
    }
}