0

WinGrid (Infragistics、知っておく必要がある場合) で、s を含む列を取得しましたint。値は、時間を計算できる秒数です。まさにそれを行う IFormatProvider/ICustomFormatter を作成しました。グリッドの初期化中に、Format および FormatInfo パラメーターを設定しました。

ただし、カスタム型フォーマッタで GetFormat が呼び出されると、型パラメータは常に NumberFormatInfo であり、ICustomFormatter ではありません。なんで?

それが役立つ場合に備えて、これが私のクラスです:

public class SecToTime : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
        {
            return this;
        }
        else
        {
            return null;
        }
    }

    public string Format(string format, object arg, IFormatProvider provider)
    {
        if (arg is int)
        {
            int seconds = (int)arg;
            int hours = (int)Math.Truncate((double)seconds / 3600);
            int minutes = (int)Math.Truncate((double)(seconds / 60) % 60);
            seconds = seconds % 60;
            return string.Format("{0:hh:mm:ss}", new DateTime(0, 0, 0, hours, minutes, seconds));
        }
        else
            throw new ArgumentNullException();
    }
}
4

1 に答える 1

0

Infragistics Team の (偉大な) Mike Salzman の言葉を引用します。

Format プロパティも設定されていない限り、FormatInfo は呼び出されません。それが呼び出されない理由を私が考えることができる唯一の理由です。

出典:このインフラジスティックス フォーラムの投稿

それをテストするには、列のFormatプロパティを何かに設定してみてください... :)

于 2011-03-09T20:43:00.363 に答える