加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF DatePicker Watermark使用错误的语言,但Dateformat是

发布时间:2020-12-15 08:47:51 所属栏目:百科 来源:网络整理
导读:我有一个非常奇怪的问题: 在我的机器上,DatePicker根据我想要设置的语言/文化更改其水印和日期格式. 如果我将我的应用程序复制到其他计算机后发生: 在某些计算机上,它的工作方式与我的机器相同.在其他计算机上只有日期格式更改,但水印没有!不用说,拥有一
我有一个非常奇怪的问题:

在我的机器上,DatePicker根据我想要设置的语言/文化更改其水印和日期格式.

如果我将我的应用程序复制到其他计算机后发生:

在某些计算机上,它的工作方式与我的机器相同.在其他计算机上只有日期格式更改,但水印没有!不用说,拥有一个例如日期选择器是非常难看的.德国日期,但英国水印.

这种行为的原因是什么?

对于i18n,我使用以下代码:

App.xaml.cs:

public partial class App : Application
{
    public App()
    {
        CultureInfo ci = new CultureInfo("de-DE"); 
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
    }
}

WindowMain.xaml.cs:

public partial class WindowMain : RibbonWindow
{
    public WindowMain()
    {
        this.Language = XmlLanguage.GetLanguage("de-DE");
        this.InitializeComponent();
    }
}

解决方法

有一点我可以说,DatePicker中的Watermark实现了bug,没有简单的访问权限.也许,由于这种困难,文本的本地化不起作用. @Matt Hamilton有一篇很棒的文章,引自 here:

Something that a lot of people (myself included) don’t like about the DatePicker,though,is that by default if no date is displayed it shows the text “Select a date” as a watermark,and this text is baked into the control – it’s not localized or accessible by any public property. This is particularly frustrating if the date in question is optional and you don’t necessarily want to prompt your users to select one.

在同一篇文章中,他提供了如何访问Watermark的决定.这里:

How to localize the WPF 4.0 DatePicker control

@Wayne Maurer以附加依赖属性的形式创建了一个通用解决方案:

<DatePicker Grid.Row="2" 
            local:DatePickerWatermarkBehaviour.Watermark="Select the date" />

您需要基于当前的文化,设置水印的文本,例如使用上面的例子.

注意:在DatePicker的Silverlight to Watermark中进行访问[link]:

DatePickerTextBox box = base.GetTemplateChild("TextBox") as DatePickerTextBox;
box.Watermark = "Type or select a date --> ";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读