String Format Not Being Applied

RogerSchlueter-7899 1,446 Reputation points
2025-06-24T22:39:57.7533333+00:00
<TextBox
    Background="#FAFAD2"
    Focusable="False"
    HorizontalContentAlignment="Center"
    Style="{StaticResource TextBoxKey}"
    Text="{Binding Path=LastUpdate, Converter={StaticResource conDate}, StringFormat='MMMM d, yyyy'}" />


Here are the relevant parts of the converter:

Private LowerLimit As New Date(2019, 12, 31)

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
	If IsDate(value) Then
		Dim val As Date = CDate(value)
		Return If(val > LowerLimit, CStr(val), String.Empty)
	Else
		Return String.Empty
	End If
End Function

And yet the displayed date is, for example, 9/1/2020

Why is the date not being displayed properly?

Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2025-06-25T03:20:08.1466667+00:00

    Try this converter:

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        If IsDate(value) Then
            Dim val As Date? = CDate(value)
            Return If(val > LowerLimit, val, Nothing)
        Else
            Return Nothing
        End If
    End Function
    

    StringFormat does not work if the value is a string, not date.

    Alternatively, you can format the date (using val.ToString("MMMM d, yyyy")) inside the converter and return the formatted string.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.