Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimePicker #4486

Open
h0545772386 opened this issue May 14, 2024 · 0 comments
Open

TimePicker #4486

h0545772386 opened this issue May 14, 2024 · 0 comments
Labels

Comments

@h0545772386
Copy link

h0545772386 commented May 14, 2024

Describe the bug

The value of SelectedDateTime of the TimePicker control is double somehow and exception falls when binding with a converter.

I save my datetime as long number yyyyMMddHHmm and convert it for display.

here is my details:

version installed:

<package id="MahApps.Metro" version="2.4.10" targetFramework="net472" />

window xmlns:

 xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

window coverter definition:

 <mah:MetroWindow.Resources>
     <local:TimeConverter x:Key="TMCNV" />
     <local:DateTimeConverter x:Key="DTCNV" />
     <local:StatConverter x:Key="STCNV" />
 </mah:MetroWindow.Resources>

control at XAML:

   <mah:TimePicker x:Name="timePopup"
                   Grid.Row="3"
                   Grid.Column="1"
                   Margin="3"
                   HorizontalAlignment="Left"
                   Width="150"
                   Culture="he-IL"
                   SelectedTimeFormat="Short"
                   SelectedDateTimeChanged="timePopup_SelectedDateTimeChanged"
                   SelectedDateTime="{Binding PHdr.TimePopup,Mode=TwoWay,Converter={StaticResource TMCNV}}" />

converter class code

internal class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((long)value == 0)
                return new DateTime(1973, 02, 07, 0, 0, 0);
            long d1 = long.Parse(DateTime.Today.ToString("yyyyMMddHHmm"));
            d1 =  d1 + (long)value;
            DateTime d2 = DateTime.Today;
            try
            {
                d2= DateTime.ParseExact(d1.ToString(), "yyyyMMddHHmm", CultureInfo.InvariantCulture);
            }
            catch { 
            }
            return d2;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return (long)0;
            return long.Parse(((DateTime)value).ToString("yyyyMMddHHmm"));
        }
    }
}

to trigger the bug use the code and try to change the Time then it will appear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant