簡體   English   中英

自 .NET 4.0 以來 XAML 綁定日期時間對象的自動更正行為?

[英]Auto correction behaviour on XAML bound datetime objects since .NET 4.0?

在將應用程序從 .NET 3.5 帶到 .NET 4.0 時,我遇到了這個特殊問題。

(文化是 nl-BE)

我將這樣的文本框(在 XAML 中)綁定到 DateTime 值,並在 PropertyChanged 上使用 UpdateSourceTrigger(LostFocus 按預期工作,但需要按您的類型進行驗證):

<TextBox Height="23" Margin="146,0,105,97.04" Name="txb_Geboortedatum" VerticalAlignment="Bottom">
        <TextBox.Text>
            <Binding Path="Geboortedatum" StringFormat="d" 
                     UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <ExceptionValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
</TextBox>

現在,當此文本框的內容為(例如)2000 年 10 月 12 日並且我想將其編輯為09/03/1981時,當我將cursor放在 2000 年底並開始“退格”時,會發生一些令人討厭的自動更正' 去掉年份值(當只留下 '2000' 的第一個數字 ('2') 時,該值會自動 - 包括 cursor 跳轉 - 再次變為 2002)。 我可以禁用此自動更正嗎?

我似乎找不到具體介紹這種行為的原因。 對於貨幣值, FormatString=c也會出現同樣的“問題”。

到目前為止我已經嘗試過:

  1. 將 FormatString 更改為更明確的內容,例如{0}{dd/MM/yyyy} (同樣的問題:當年份剩下 2 位數時開始自動更正)。
  2. 禁用我添加到我的 App.xaml.cs 的以下代碼段:

     FrameworkElement.LanguageProperty.OverrideMetadata( typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage( CultureInfo.CurrentCulture.IetfLanguageTag)));

首先包含此代碼段的原因: 查看此鏈接

我在這里遺漏了一些明顯的東西嗎? 我無法在 3.5 中重現這一點。 我真的必須推出自己的 ValueConverters 才能使其正常工作嗎? 這看起來像是從 3.5 sp 1 中引入的StringFormat

來自DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns('d') Output 看起來確實略有不同,但沒有什么可以立即解釋這種行為(可能不相關):

.NET 3.5        .NET 4.0

d/MM/yyyy       d/MM/yyyy
d/MM/yy         d/MM/yy
dd-MM-yy        dd-MM-yy
dd.MM.yy        dd.MM.yy
yyyy-MM-dd      dd.MMM.yyyy
                yyyy-MM-dd

我現在最終使用了這樣的東西,但我會對解決上述問題的其他方法非常感興趣:

public class CustomDateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
                          CultureInfo culture)
    {
        if (value == null) { return ""; }
        DateTime dt;
        if (DateTime.TryParse(value.ToString(), CultureInfo.CurrentCulture, 
                              DateTimeStyles.None, out dt))
        {
            return dt.ToShortDateString();
        }
        return "";
    }

    public object ConvertBack(object value, Type targettype, object parameter, 
                              CultureInfo culture)
    {
        if (value == null || value.ToString().Trim().Length==0) { return null; }
        string frmt = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
        DateTime dt;
        if (DateTime.TryParseExact(value.ToString(), frmt, 
                                   CultureInfo.CurrentCulture, 
                                   DateTimeStyles.None, out dt))
        {
            return dt;
        }
        return DependencyProperty.UnsetValue;
    }
}

好吧,如果您在 PropertyChanged 上自動更正,這種行為是“正常的”。 當您開始退格時,傳遞給 DateTime 構造函數的值是:

屏幕>>日期時間(' | '是每個退格之前的 cursor 位置)

10-12-2000| >> DateTime(200,12,10) >> 10-12-0200

10-12-020|0 >> DateTime(020,12,10) >> 10-12-020 

10-12-00|20  >> DateTime(020,12,10) >> 10-12-020 

10-12-0|20 >> DateTime(20,12,10) >> 10-12-2020

(由於yy-mm-dd字符串格式,我認為這可能會因您的文化而異)

很難,我不太明白為什么 new DateTime(20,12,10) 在文本框中為我提供 2020 年。 當我使用 DateTime.Parse(20,12,10) 它給我 2012-10-20 (yyyy-mm-dd)。 除非我們使用自定義 ValueConvertor,否則必須有某種我們無法控制的自動轉換或解析

我認為自定義轉換器是 go 如果你絕對需要'as-you-type validation'的好方法,否則 OnLostFocus 就像你說的那樣工作得很好。

順便說一句,我的文化是美國

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM