0

WPF-DataGrid の FormatString に小さな問題があります。列には DataGridTextColumn を使用します。私のデータソースはIList<IDictionary<string, string>>

私の問題は、設定した場合StringFormat、列に影響がないことです。

これは私のバインディングコードです:

cColumn.Binding = new Binding("[" + Config.InternName.ToLower() + "]");

そして、これが私のFormatStringの設定方法です:

    #region [Date-Time Formating]
    if (Config.DateTimeFormat.Trim() != "")
    {
        string cDateTimeFormat = Config.DateTimeFormat.Trim();
        (cColumn.Binding as Binding).StringFormat = cDateTimeFormat;
    }
    #endregion

私は多くの DateTime-StringFomrats を試しましたが、私の問題を解決するものは何もありません。

ありがとうございました!

4

1 に答える 1

0

これが私の解決策です。日時をバインドせずに、単純に FormatString を使用することはできません。だから私は IValueconverter を書きました:

public class StringDateTimeValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DateTime cOutDateTime = DateTime.Now;

        if (DateTime.TryParse(value.ToString(), out cOutDateTime))
        {
            return cOutDateTime;
        }
        else
        {
            return DependencyProperty.UnsetValue;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }
}
于 2013-09-03T10:20:14.783 に答える