Reputation: 101
I have this weird behavior where I load data from a DataView
to a DataGrid
. I have a custom DataGridBoundColumn
with a ContentTemplateSlector
. The selector work very well, but as the columns receive directly the value as DateTime
I do not specify a property, just Binding
.
The d
of DateTime.StringFormat
does not do anything, but a dd/MM/yyyy
works just fine. Same issue for Double
. Even tried {}{0:D}
but {##0.00##}
works fine.
Any clue for the weird behavior?
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);
Selector:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if(item is DateTime)
{
return DateTemplate;
}
}
The xaml part that does not work:
<DataTemplat x:Key="DateTemplate">
<TextBlock Text="{Binding StringFormat=d}"/>
</DataTemplate>
Does work:
<DataTemplat x:Key="DateTemplate">
<TextBlock Text="{Binding StringFormat=dd/MM/yyyy}"/>
</DataTemplate>
Thank you,
Upvotes: 0
Views: 122
Reputation: 22119
Same issue for Double. Even tried {}{0:D} but {##0.00##} works fine.
The D
/d
format specifier is only applicable to integral types, not double
, use F
/f
instead, e.g.:
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.
I think that you are mixing up two concepts. The d
used in StringFormat
like below is a standard format specifier that will format the date to use the short date pattern, like 6/15/2009 (en-US), see the documentation.
StringFormat=d
StringFormat={}{0:d}
On the other hand, the string format below is a custom format, where d
has a different meaning stands for the day of the month.
StringFormat={}{0:MM/dd/yy H:mm:ss zzz}}
The confusing thing about this is that you cannot use d
only like above and expect it to be the day of the month, because it will be interpreted as the standard format specifier.
If the "d" format specifier is used without other custom format specifiers, it's interpreted as the "d" standard date and time format specifier.
Upvotes: 2