How to show Persian date in WPF DataGrid?

这一生的挚爱 提交于 2019-12-23 19:48:46

问题


How can I change this code (or somewhere else):

<DataGridTemplateColumn x:Name="timeColumn" Header="time" IsReadOnly="True">
    <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
          <DatePicker SelectedDate="{Binding time, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
       </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

To show date in Persian format? time in database is in Timestamp format. I want just "show it" in Persian format, and have a Persian calendar if possible.


回答1:


If you only want to show PersianDateTime you can use System.Globalization.PersianCalendar in your view-model like this:

    public string PersianDate
    {
        get
        {
            PersianCalendar pc = new PersianCalendar();
            DateTime thisDate = convert your Timestamp to DateTime here ...;
            return string.Format("{0}, {1}/{2}/{3} {4}:{5}:{6}",
                          pc.GetDayOfWeek(thisDate),
                          pc.GetMonth(thisDate),
                          pc.GetDayOfMonth(thisDate),
                          pc.GetYear(thisDate),
                          pc.GetHour(thisDate),
                          pc.GetMinute(thisDate),
                          pc.GetSecond(thisDate));
        }
    }

And then in your Xaml:

    <DataGrid ItemsSource="{Binding YourList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="PersianTime" Binding="{Binding PersianDate}"/>
            < Other columns ... />
        </DataGrid.Columns>
    </DataGrid>

If you need a Persian calendar in Farsi format and language, you may want to check out this library, another good source is this link from Code Project.




回答2:


As for me, changing environment to change DatePicker format (like Thread.CurrentCulture) is not a good idea. Sure, you can create Control derived from DatePicker and implement dependency property like Format, but this costs too much effort. Simple and elegant solution I found is binding value not to SelectedDate itself, but to some unused property (I used ToolTip property for this) and update this property when SelectedDate is changed. C# implementation for one-way binding looks like this:

 DatePicker datePicker = new DatePicker();
    datePicker.SetBinding(ToolTipProperty, "Date");
    datePicker.SelectedDateChanged += (s, ea) =>
        {
            DateTime? date = datePicker.SelectedDate;
            string value = date != null ? date.Value.ToString("yyyy-MM-dd") : null;
            datePicker.ToolTip = value;
        };

XAML+C# should look like this:

XAML:

<DatePicker ToolTip="{Binding Date Mode=TwoWay}"
            SelectedDateChanged="DatePicker_SelectedDateChanged"/>

C#:

private void DatePicker_SelectedDateChanged(object sender, EventArgs ea)
{
    DatePicker datePicker = (DatePicker)sender;
    DateTime? date = datePicker.SelectedDate;
    string value = date != null ? date.Value.ToString("yyyy-MM-dd") : null;
    datePicker.ToolTip = value;
}

For two-way implementation handle ToolTipChanged event the same way to update SelectedDate.

       </Setter>
    </Style>
</DatePicker.Resources>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>


来源:https://stackoverflow.com/questions/31780253/how-to-show-persian-date-in-wpf-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!