文化不变的 StringFormat 不起作用

Culture invariant StringFormat does not work

我有这种奇怪的行为,我将数据从 DataView 加载到 DataGrid。我有一个带有 ContentTemplateSlector 的自定义 DataGridBoundColumn。选择器工作得很好,但是由于列直接接收值 DateTime 我没有指定 属性,只是 Binding.

DateTime.StringFormatd 没有任何作用,但是 dd/MM/yyyy 可以正常工作。 Double 同样的问题。甚至尝试 {}{0:D}{##0.00##} 工作正常。

有任何关于奇怪行为的线索吗?

protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
   Binding binding = new Binding((Binding).Path.Path(){Source= dataItem};

   ContentControl content= new ContentControl
   {
      ContentTemplateSelector = (TableDataPointConvert)cell.FindResource("TemplateSelector")
   };

   content.SetBinding(ContentControl.ContentProperty, binding);

选择器:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
   if(item is DateTime)
   {
      return DateTemplate;
   }
}

不工作的xaml部分:

<DataTemplat x:Key="DateTemplate">
  <TextBlock Text="{Binding StringFormat=d}"/>
</DataTemplate>

有效:

<DataTemplat x:Key="DateTemplate">
  <TextBlock Text="{Binding StringFormat=dd/MM/yyyy}"/>
</DataTemplate>

谢谢,

Same issue for Double. Even tried {}{0:D} but {##0.00##} works fine.

D/dformat specifier只适用于整数类型,不适用double,使用F/f 代替,例如:

StringFormat=F
StringFormat={}{0:F}}
StringFormat=F0
StringFormat={}{0:F0}}
...

The "d" of DateTime.StringFormat does not do anything, but a dd/MM/yyyy works just fine.

我认为您混淆了两个概念。 StringFormat 中使用的 d 如下所示是标准格式说明符,它将格式化日期以使用 short date pattern, like 6/15/2009 (en-US), see the documentation.

StringFormat=d
StringFormat={}{0:d}

另一方面,下面的字符串格式是 custom format,其中 d 具有不同的含义,代表一个月中的第几天。

StringFormat={}{0:MM/dd/yy H:mm:ss zzz}}

令人困惑的是,您不能像上面那样只使用 d 并期望它是一个月中的某一天,因为它将 interpreted 作为标准格式说明符。

If the "d" format specifier is used without other custom format specifiers, it's interpreted as the "d" standard date and time format specifier.