フォーム要素を取得する
値の取得
フォームのコントロールを取得する際、フォーム要素をフラットに取得する場合、IFormオブジェクトのElementsプロパティを用います。
public void EnumerateElements(ICommandContext c, ICommandParams p)
{
    IForm form = c.App.Workspace.CurrentEditor as IForm;
    if (form == null) return;
    // 表示されているフォームのコントロールを取得します
    IFormElementCollection elements = form.Elements;
    c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
    foreach ( IFormElement element in elements )
    {
        // コントロールの型を出力します
        c.App.Output.WriteLine("sample", $"  Element: {element.ControlType}");
    }
}
上記のサンプルコードでは試験的に実装された次のAPIを使用しています。
- IEditorDef.Name プロパティ
これらのAPIは品質保証しておらず、API仕様にも未記載のものです。ご利用される場合はユーザー様の責任でご利用ください。
フォーム要素を木構造で取得したい場合、IFormElement.Controlsを用います。フォームのルートコントロールをIFormオブジェクトのRootControlプロパティで取得し、そこから再帰的に取得する必要があります。
public void EnumerateFormElements(ICommandContext c, ICommandParams p)
{
    IForm form = c.App.Workspace.CurrentEditor as IForm;
    if (form == null) return;
    // 表示されているフォームのルートコントロールを取得します
    IFormElement rootControl = form.RootControl;
    c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
    
    // 再帰的にフォーム要素を取得します
    var formElements = GetRecursivelyFormElements(c, rootControl, 0);
    foreach(IFormElement element in formElements)
    {
        // ...
    }
}
// 再帰的にフォーム要素を取得します
private IEnumerable<IFormElement> GetRecursivelyFormElements(ICommandContext c, IFormElement element, int hierarchy)
{
    var empty = new string(' ', hierarchy);
    var elementDefinitionName = element.ElementDefinition == null ? "要素定義なし" : element.ElementDefinition.Name;
    c.App.Output.WriteLine("sample", $"{empty}ColumnType: {element.ControlType}, Element: {elementDefinitionName}");
    if (!element.Controls.Any())
    {
        return Enumerable.Empty<IFormElement>();
    }
    return new []{ element }.Union(element.Controls.SelectMany(control => GetRecursivelyFormElements(c, control, hierarchy + 1)));
}
上記のサンプルコードでは試験的に実装された次のAPIを使用しています。
- IEditorDef.Name プロパティ
- IElementDef.Name プロパティ
これらのAPIは品質保証しておらず、API仕様にも未記載のものです。ご利用される場合はユーザー様の責任でご利用ください。
コントロールから値を取得する場合、IEditorElementオブジェクトのGetValueメソッドを用います。
public void EnumerateElements(ICommandContext c, ICommandParams p)
{
    // 中略
    foreach ( IFormElement element in elements )
    {
        c.App.Output.WriteLine("sample", $"  Element: {element.ControlType}");
        var value = element.GetValue();
        if(value is IModel model)
        {
            // モデルを表現するコントロールの場合は、 モデルが取得できます
            c.App.Output.WriteLine("sample", $"    Class: {model.ClassName}, Name: {model.Name}");
            continue;
        }
        
        // フィールド値を表現するコントロールの場合 は、 フィールド値が取得できます
        c.App.Output.WriteLine("sample", $"    Value: {value}");
    }
}
グリッド列の取得
フォームの要素がグリッド (コントロールの型がIGrid型) の場合、IGridオブジェクトのColumnsプロパティを用いることで列情報を取得できます。
public void EnumerateGridColumns(ICommandContext c, ICommandParams p)
{
    IForm form = c.App.Workspace.CurrentEditor as IForm;
    if (form == null) return;
    // 表示されているフォームのコントロールを取得します
    IFormElementCollection elements = form.Elements;
    c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
    foreach ( IFormElement element in elements )
    {
        c.App.Output.WriteLine("sample", $"  Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
        if(element is IGrid grid)
        {
            // コントロールがグリッドの場合、列情報を出力します
            foreach(IGridColumn column in grid.Columns)
            {
                c.App.Output.WriteLine("sample", $"    ColumnType: {column.DataTypeName}, Path: {column.Path}");
            }
        }
    }
}
上記のサンプルコードでは試験的に実装された次のAPIを使用しています。
- IEditorDef.Name プロパティ
- IElementDef.Name プロパティ
これらのAPIは品質保証しておらず、API仕様にも未記載のものです。ご利用される場合はユーザー様の責任でご利用ください。
グリッド行の取得
IGridオブジェクトのRowsプロパティを用いることでグリッドから行オブジェクトも取得できます。
public void EnumerateGridRows(ICommandContext c, ICommandParams p)
{
    IForm form = c.App.Workspace.CurrentEditor as IForm;
    if (form == null) return;
    // 表示されているフォームのコントロールを取得します
    IFormElementCollection elements = form.Elements;
    // コントロールごとに処理をします 
    c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
    foreach ( IFormElement element in elements )
    {
        c.App.Output.WriteLine("sample", $"  Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
        if(element is IGrid grid)
        {
            foreach(IGridRow row in grid.Rows)
            {
                // コントロールがグリッドの場合、行に対応するモデル名を出力します
                c.App.Output.WriteLine("sample", $"    RowName: {row.Model.Name}");
            }
        }
    }
}
上記のサンプルコードでは試験的に実装された次のAPIを使用しています。
- IEditorDef.Name プロパティ
- IElementDef.Name プロパティ
これらのAPIは品質保証しておらず、API仕様にも未記載のものです。ご利用される場合はユーザー様の責任でご利用ください。
グリッドのセルの値の取得
グリッド行からセルの値を取得した場合、IGridCellオブジェクトのGetValueメソッドを用います。
public void EnumerateGridRows(ICommandContext c, ICommandParams p)
{
    IForm form = c.App.Workspace.CurrentEditor as IForm;
    if (form == null) return;
    // 表示されているフォームのコントロールを取得します
    IFormElementCollection elements = form.Elements;
    // コントロールごとに処理をします 
    c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
    foreach ( IFormElement element in elements )
    {
        c.App.Output.WriteLine("sample", $"  Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
        if(element is IGrid grid)
        {
            foreach(IGridRow row in grid.Rows)
            {
                // コントロールがグリッドの場合、行に対応するモデル名を出力します
                c.App.Output.WriteLine("sample", $"    RowName: {row.Model.Name}");
                IGridCellCollection cells = row.Cells;
                foreach(IGridCell cell in cells)
                {
                    // グリッドにセルが存在する場合、セルの値を出力します
                    var value = cell.GetValue();
                    if(value is IModel model)
                    {
                        c.App.Output.WriteLine("sample", $"      Cell: {model.Name}");
                        continue;
                    }
                    c.App.Output.WriteLine("sample", $"      Cell: {value}");
                }
            }
        }
    }
}
上記のサンプルコードでは試験的に実装された次のAPIを使用しています。
- IEditorDef.Name プロパティ
- IElementDef.Name プロパティ
これらのAPIは品質保証しておらず、API仕様にも未記載のものです。ご利用される場合はユーザー様の責任でご利用ください。