Question

I try to display a datetime in the French format dd/MM/AAAA hh:mm:ss.
I have a DateTime in the English format like MM/dd/AAAA hh:mm:ss AM or PM
So I do a parsing like this:

foreach (var _element in listElement)
{
    IFormatProvider culture = new CultureInfo("fr-Fr");
    string date = _element.DateModifElement.ToString();
    _element.DateModifElement = DateTime.Parse(date, culture, DateTimeStyles.NoCurrentDateDefault);

    listElementCollection.Add(_element);
}

It works perfectly, so now my DateTime property in my objects has the French format. So I only need to display the date in the view.

So my ObservableCollection is binded to a LongListSelector in the view like this:

 ObsvCollectionBdeskElement =new ObservableCollection<GroupType<BdeskElement>>(listElementCollection);
llsElements.ItemsSource = ObsvCollectionBdeskElement;

In the xaml

<phone:LongListSelector 
    x:Name="llsElements" 
    ItemsSource="{Binding}"
    IsGroupingEnabled="True"
    HideEmptyGroups="False"
    JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"
    LayoutMode="List">

<phone:LongListSelector.ItemTemplate>
    <DataTemplate>  
        <Grid Margin="0,20,0,0" Background="White">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu IsZoomEnabled="True" x:Name="ContextMenu" Background="#FF00485A" >
                <toolkit:MenuItem Header="renommer" Click="renommer_Click" Foreground="White" />
                <toolkit:MenuItem  Header="supprimer"  Click="supprimer_Click" Foreground="White" />
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>

        <Grid VerticalAlignment="Center" Grid.Column="1" Margin="10,0,20,0"  >
            <TextBlock Grid.Row="1" Margin="0,2,0,0" Text="{Binding DateModifElement}" Foreground="{StaticResource C01}" FontSize="16"/>
        </Grid>  

And the main Grid(layout) has a datacontext which is define by the type of my object. But the problem is that the view still display the english format. So i thought it was the culture of the App which was remmained in English but no, I checked with this line of code and the value is "fr-FR".

string info = CultureInfo.CurrentCulture.Name;
Was it helpful?

Solution

It's a known issue, the binding doesn't use the system current culture to format values. You can create you own value converter to implement it.

public class FrDateValueConverter
        : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var date = (DateTime) value;
                return date.ToString(new CultureInfo("fr-Fr"));
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

now you should add the converter to the resources

<FrDateValueConverter x:Key="MyConv" />

finally you should specify Converter parameter of the binding

<TextBlock Grid.Row="1" Margin="0,2,0,0" Text="{Binding DateModifElement, Converter={StaticResource MyConv}}" Foreground="{StaticResource C01}" FontSize="16"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top