0

I have following problem.

My application shows some data in datagrid and one of the datagrid columns contains date and time that is shown in system format.

When I change system format of date or time (Change date and time settings -> Change date and time -> Change calendar settings -> Time) and restart my application date and time format in mentioned column should be changed also.

On client side in XAML I have:

<resCtrl:ExtendedDataGridTextColumn Width="130" ElementStyle="{StaticResource columnStyle}"  Header="{Binding Path=Resources.App_RECORD_DATE_TIME, Source={StaticResource LocalizedStrings} }" HorizontalAlignment="Left" VerticalAlignment="Center" Binding="{Binding Path=RecordDateTime, Converter={StaticResource DateTimeConverter}, UpdateSourceTrigger=PropertyChanged}"/>

Converter looks like:

public class DateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || ((DateTime)value) == new DateTime())
        {
            return " - ";
        }
        else
        {
            return ((DateTime)value).ToLocalTime().ToString();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

And all that WORKS on client side. When I change format in windows, format is updated in table. But I have another datagrid, which has column that shows some text which contains date. So on server side, i prepare date and send only string to the client.

But on server side, I can't get system format for date - no matter what I set in control panel. Code is absolutely same, but doesn't work.

string text = someDate.ToLocalTime().ToString() + " dsfdsfds";

I tried something with CultureInfo and formats, but I can't predefine any format because I want system format - not some predefined. Do you have any solution?

mm8
  • 163,881
  • 10
  • 57
  • 88
gzsun
  • 209
  • 1
  • 11
  • You *do* get the system's format. The *system* in this case is the *server*. If you want to use the client's locale, *don't* generate any strings on the server. Bind to a `DateTime` property. Use the binding's format string to control how the date appears – Panagiotis Kanavos Feb 14 '17 at 12:19
  • BTW a DateTime object is also *smaller* than the equivalent string representation. Just 8 bytes – Panagiotis Kanavos Feb 14 '17 at 12:20
  • Why the call to `ToLocalTime()`? This call modifies the *timezone offset*. This only makes sense if the property is in UTC. – Panagiotis Kanavos Feb 14 '17 at 12:22
  • Check [this duplicate question](http://stackoverflow.com/questions/19278515/use-stringformat-to-add-a-string-to-a-wpf-xaml-binding). It shows how you can use the `StringFormat` binding property. You could just write `Binding="{Binding Path=RecordDateTime, StringFormat={}{0:D}}" or any other format string, eg `yyyy-MM-dd`. You don't need a converter to format – Panagiotis Kanavos Feb 14 '17 at 12:25
  • Possible duplicate of [Use StringFormat to add a string to a WPF XAML binding](http://stackoverflow.com/questions/19278515/use-stringformat-to-add-a-string-to-a-wpf-xaml-binding) – Panagiotis Kanavos Feb 14 '17 at 12:25
  • Thank you Panagiotis. Complete application is much more complex, I tried just to simplify things in description. Now I understand the problem, but at this moment I can't change contracts between client and server, so I must devise new solution. – gzsun Feb 14 '17 at 13:23
  • Obviously the server cannot be supposed to know that the local settings are on a client computer. There may be many clients with different formats... – mm8 Feb 14 '17 at 14:57
  • Yeah, only way to resolve this is message creation on client side. – gzsun Feb 14 '17 at 16:03

0 Answers0