更改 WPF DatePicker 的字符串格式
我需要更改 WPF 工具包 DatePicker 中 DatePickerTextBox 的字符串格式,以使用连字符而不是斜杠作为分隔符。
有没有办法覆盖这个默认区域性或显示字符串格式?
01-01-2010
I need to change the string format of the DatePickerTextBox in the WPF Toolkit DatePicker, to use hyphens instead of slashes for the seperators.
Is there a way to override this default culture or the display string format?
01-01-2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我在这段代码的帮助下解决了这个问题。希望它也能对大家有所帮助。
I have solved this problem with a help of this code. Hope it will help you all as well.
根据 Wonko 的回答,您似乎无法以 Xaml 格式或通过从 DatePicker 继承来指定日期格式。
我已将以下代码放入 View 的构造函数中,该构造函数将覆盖当前线程的 ShortDateFormat:
It appears, as per Wonko's answer, that you cannot specify the Date format in Xaml format or by inheriting from the DatePicker.
I have put the following code into my View's constructor which overrides the ShortDateFormat for the current thread:
WPF 工具包
DateTimePicker
现在具有一个Format
属性和一个FormatString
属性。如果您指定Custom
作为格式类型,则可以提供您自己的格式字符串。The WPF Toolkit
DateTimePicker
now has aFormat
property and aFormatString
property. If you specifyCustom
as the format type, you can provide your own format string.接受的答案(感谢@petrycol)让我走上了正确的道路,但我在实际的日期选择器中得到了另一个文本框边框和背景颜色。使用以下代码修复了它。
The accepted answer (thanks @petrycol) put me on the right track, but I was getting another textbox border and background color within the actual date picker. Fixed it using the following code.
注意:这个答案(最初写于 2010 年)适用于早期版本。查看有关在较新版本中使用自定义格式的其他答案
不幸的是,如果您正在谈论 XAML,则您必须将 SelectedDateFormat 设置为“Long”或“Short”。
如果您下载了工具包的源代码以及二进制文件,您可以看到它是如何定义的。以下是该代码的一些亮点:
DatePicker.cs
DatePickerFormat.cs
NOTE: This answer (originally written in 2010) is for earlier versions. See other answers for using a custom format with newer versions
Unfortunately, if you are talking about XAML, you are stuck with setting SelectedDateFormat to "Long" or "Short".
If you downloaded the source of the Toolkit along with the binaries, you can see how it is defined. Here are some of the highlights of that code:
DatePicker.cs
DatePickerFormat.cs
XAML
C#
在 ToString("") 中放置您想要的任何格式,例如 ToString("dd MMM yyy") ,输出格式将为 7 Jun 2017
XAML
C#
put what ever format you want in ToString("") for example ToString("dd MMM yyy") and output format will be 7 Jun 2017
我已经用一种简单的方法解决了这个问题......设置选项语言控制。
I have solved this problem in an easy way ... Setting options Language in control.
转换器类:
wpf标签
希望有帮助
Converter class:
wpf tag
Hope it helps
显示的格式取决于位置,但这可以通过编写以下内容来避免:
And you will be happy!(dd'-'MM'-'yyy)
Format exhibited depending on the location but this can be avoided by writing this:
And you will be happy!(dd'-'MM'-'yyy)
正如 Ben Pearce 回答的那样,我们可以使用 CultureInfo 类来处理自定义格式,
我真的认为这是唯一合乎逻辑的方法。如果您有不同的 dateTimePickers,它们具有不同的 Format,您可以使用:
然后您可以更改 .xaml 文档中的 DateFormat。
As Ben Pearce answered we can use the CultureInfo Class to take care of the Custom Format,
I really think is the only logical way. And if you have different dateTimePickers which have different Format, you can use:
And then you can just change the DateFormat in the .xaml document.