How to show date by formatting?

fatih uyanık 225 Reputation points
2023-11-09T20:04:30.9+00:00

I have a model like below. On the Xaml side, I show the CreatedAt property in the ListView. However, this property must be formatted and displayed according to the current culture. When I do it on the XAML side, I have to specify a fixed format. I wonder how I can format the date and display it via CreateAt in a culturally sensitive manner? Thanks.

using System;

namespace Akıllı_Notlar.Models
{
    internal class Note
    {
        private int _ıd;
        private string _title;
        private string _content;
        private DateTime _createdAt;
        private DateTime? _updatedAt;

        public int Id { get => _ıd; set => _ıd = value; }

        public string Title { get => _title; set => _title = value; }

        public string Content { get => _content; set => _content = value; }

        public DateTime CreatedAt { get => _createdAt; set => _createdAt = value; }

        public DateTime? UpdatedAt { get => _updatedAt; set => _updatedAt = value; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }
}

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2023-11-09T20:59:36.36+00:00

    I'm not too familiar with WPF but this SO post suggests using the StringFormat binding flag:

    https://stackoverflow.com/questions/5046429/wpf-binding-stringformat-short-date-string#answer-5046460

    e.g.

    <TextBlock Text="{Binding CreatedAt, StringFormat=d}" />
    

    Provided that you've set the current thread's culture to your user's culture this should hopefully work:

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
    

    Is this what you've already tried?


  2. gekka 12,206 Reputation points MVP Volunteer Moderator
    2023-11-10T10:09:44.1566667+00:00

    Sample using Language property .

    <Window x:Class="Gekka.WpfApplication1.MainWindow1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Gekka.WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow2" Width="200" FontSize="16" SizeToContent="Height">
        <StackPanel >
            <FrameworkElement.FlowDirection>
                <Binding ElementName="combo1" Path="SelectedItem">
                    <Binding.Converter>
                        <local:FlowDirectionConverter />
                    </Binding.Converter>
                </Binding>
            </FrameworkElement.FlowDirection>
            
            <ListView ItemsSource="{Binding}" Height="100" >
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Path=Title}" />
                            <GridViewColumn Header="CreatedAt"
                                            DisplayMemberBinding="{Binding Path=CreatedAt,StringFormat=d}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
            
    
            <ComboBox x:Name="combo1" SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window},Path=Language}">
                <ComboBox.ItemsSource>
                    <x:Array Type="{x:Type x:XmlLanguage}">
                        <x:XmlLanguage>ar-ae</x:XmlLanguage>
                        <x:XmlLanguage>en-us</x:XmlLanguage>
                        <x:XmlLanguage>fr-fr</x:XmlLanguage>
                        <x:XmlLanguage>ja-jp</x:XmlLanguage>
                    </x:Array>
                </ComboBox.ItemsSource>
            </ComboBox>
        </StackPanel>
    
    </Window>
    
    namespace Gekka.WpfApplication1
    {
        using System;
        using System.Globalization;
        using System.Linq;
        using System.Windows;
        using System.Windows.Controls;
    
        public partial class MainWindow1 : Window
        {
            public MainWindow1()
            {
                InitializeComponent();
    
                this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentUICulture.IetfLanguageTag);
    
                this.DataContext = new Note[] { new Note() };
            }
    
            class Note
            {
                public string Title { get; set; } = "Test";
                public DateTime? CreatedAt { get; set; } = DateTime.Now;
            }
        }
    
        class FlowDirectionConverter : System.Windows.Data.IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value is System.Windows.Markup.XmlLanguage xl)
                {
                    value = xl.GetEquivalentCulture();
                }
    
                if (value is System.Globalization.CultureInfo ci)
                {
                    if (ci.TextInfo.IsRightToLeft)
                    {
                        return FlowDirection.RightToLeft;
                    }
                }
                return FlowDirection.LeftToRight;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    
    0 comments No comments

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.