FlexGrid for WPF
行番号を表示する
基本操作 > 行 > 行番号を表示する

FlexGrid for WPFCellFactory クラスを使用することでオートナンバーと同様の処理を単純に実現できます。また、CreateRowHeaderContent メソッドをオーバーライドして固定行の場合も行番号を表することが可能です。

次のコードでは、グリッドをデータソースと連結していることを前提として、CellFactory クラスを継承したカスタムセルファクトリに行番号を設定する処理を追加します。

【実行例】

 

コードのコピー
'CellFactoryクラスを継承して、カスタムセルファクトリを使用します
Private Sub ProcessFlexGrid(C1FlexGrid As C1.WPF.FlexGrid.C1FlexGrid)
    C1FlexGrid.CellFactory = New Custom1CellFactory()
End Sub
Private Class Custom1CellFactory
    Inherits CellFactory
    Public Overrides Sub CreateRowHeaderContent(grid As C1FlexGrid, bdr As Border, range As CellRange)
        Dim row = grid.Rows(range.Row)
        Dim textblock__1 = New TextBlock()
        Dim binding = New Binding() With { _
            Key .Source = row, _
            Key .Path = New PropertyPath("Index"), _
            Key .Converter = New IndexValueConverter() _
        }
        textblock__1.SetBinding(TextBlock.TextProperty, binding)
        bdr.Child = textblock__1
    End Sub
End Class
'行インデックスを1から表示するように設定します
Public Class IndexValueConverter
    Implements IValueConverter
    Private Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim index As Integer = CInt(value)
        Return index + 1
End Function
    Private Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class
コードのコピー
//CellFactoryクラスを継承して、カスタムセルファクトリを使用します
private void ProcessFlexGrid(C1.WPF.FlexGrid.C1FlexGrid C1FlexGrid)
{
    C1FlexGrid.CellFactory = new Custom1CellFactory();
}
class Custom1CellFactory : CellFactory
{
    public override void CreateRowHeaderContent(C1FlexGrid grid, Border bdr, CellRange range)
    {
        var row = grid.Rows[range.Row];
        var textblock = new TextBlock();
        var binding = new Binding() { Source = row, Path = new PropertyPath("Index"), Converter = new IndexValueConverter() };
        textblock.SetBinding(TextBlock.TextProperty, binding);
        bdr.Child = textblock;
    }
}
//行インデックスを1から表示するように設定します
public class IndexValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int index = (int)value;
        return index + 1;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}