WPF 工具包 DatePicker 仅月/年
我正在使用上述工具包的日期选择器,但我想将其限制为仅选择月份和年份,因为在这种情况下,用户不知道或不关心确切的日期。显然,数据存储在日期时间中格式将存储日期,但这与我无关。有没有一种简单的方法可以解决这个问题?
谢谢
I'm using the Toolkit's Datepicker as above but I'd like to restrict it to month and year selections only, as in this situation the users don't know or care about the exact date .Obviously with the data being stored in a Datetime format there will be day stored but that doesn't concern me. Is there an easy way to tie this down?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是Simon的和博士。 ABT 的 答案,包括所有额外必需的代码以及修复了
Windows
和Microsoft
控件之间的引用冲突。这需要 345 行的类才能实现,这非常荒谬,但如果您包含
DatePickerCalendar.cs
(带有您的命名空间)、构建您的项目并使用以下 XAML,它应该可以工作。DatePickerCalendar.cs
屏幕截图
This is a combination of Simon's and Dr. ABT's answers, including all extra required code and with the reference conflicts between
Windows
andMicrosoft
controls fixed.It's pretty ridiculous that this requires a class of 345 lines to achieve, but it should work if you include
DatePickerCalendar.cs
(with your namespace), build your project, and use the following XAML.DatePickerCalendar.cs
Screenshot
我将 Simon 的答案翻译成 VB,我在这里发布结果(请注明他)
但是对于格式类,我无法让它工作。
我使用并稍微修改了 petrycol 的(更简单的)答案来显示月/年。来源:
更改 WPF DatePicker 的字符串格式
i translated Simon's answer into VB, i post here the results (please give credits to him )
But for the format class, i couldn't get it to work.
I used and slighlty modified the (simpler) answer from petrycol for the month / year display. Source :
Changing the string format of the WPF DatePicker
为了添加 Simon 的精彩答案,我对 DatePickerDateFormat.cs 进行了一些更改,以防止文本框短暂闪烁的错误dd/MM/yyyy(原始字符串格式)和 MM/yyyy(覆盖字符串格式)之间。
发生这种情况是因为 DatePicker 在打开下拉列表之前立即在内部设置了 PART_Textbox 文本。它将其设置为字符串格式的文本,您对此无能为力 - 它将覆盖您的绑定。
为了防止这种行为,我将此代码添加到上面的DatePickerDateFormat 类中。
在ApplyDateFormat中,获取下拉按钮并处理PreviewMouseUp
当PreviewMouseUp触发时,如果有选定的日期,则覆盖On Dropdown Shown行为(我们模仿Datepicker所做的一切,但我们不设置PART_TextBox Text)
,其中扩展方法 TryFindAncestorOrSelf 沿着可视化树向上查找 T 类型的对象。您可以在此处找到其实现:http://www.hardcodet.net/2008/02/find-wpf-parent
希望这对某人有帮助!
To add to Simon's brilliant answer, I've made some changes to DatePickerDateFormat.cs to prevent a bug where the Textbox momentarily flickers between dd/MM/yyyy (original string format) and MM/yyyy (overridden string format).
This occurs due to the DatePicker setting the PART_Textbox text internally immediately before the dropdown is opened. It sets it to the string formatted text and there is ntohing you can do about it - it will override your binding.
To prevent this behaviour, I added this code to the DatePickerDateFormat class from above.
in ApplyDateFormat, get the dropdown button and handle PreviewMouseUp
When PreviewMouseUp fires, if there is a Selected date, override the On Dropdown Shown behaviour (we mimic everything that the Datepicker does, but we don't set the PART_TextBox Text)
where the extension method TryFindAncestorOrSelf walks up the visual tree to find an object of type T. You can find an implementation of this here: http://www.hardcodet.net/2008/02/find-wpf-parent
Hope this helps someone!
最近我对日历有一个特定的要求,可以选择仅月份和年份。所以我之前有一篇关于 wpf 日期选择器的自定义格式的文章,但在这种情况下我们需要做一些棘手的答案。首先,您需要创建一个特定的控件,如下所示:
一旦您已经这样做了。当其他人存在另一篇文章时,我不记得该文章的 url,但无论如何,我认为您需要像这样覆盖 OnCalendarOpened 方法:
请注意,我们添加了最后一行,其中包含事件 DisplayModeChanged 的处理程序。这对于接下来的步骤非常重要。好的,下一步是定义 DisplayModeChanged
这里有几件事,首先您需要选择一个月,这就是创建函数 GetSelectedMonth 的原因。第二件事是该方法使用 WPFToolkit 的一个名为 DateTimeHelper.cs 的类。好的,到目前为止,我们刚刚创建了提取月份的功能。但是,一旦您单击月历,我们就会继续以月/年格式显示所选日期。为了实现这一点,我们只需要为我们的自定义控件创建一个特定的样式,如下所示:
在我的第一个回答中
如何更改 WPF 应用程序中 DateTimePicker 的格式(例如 dd/MMM/yyyy)
我用日期选择器的自定义格式解释了所有内容。我希望这对你有帮助。请任何评论或建议,请告诉我,问候!
Recently I had a specific requirement for calendar with the option to choose only month and year. So i have a previous post about custom format for the wpf datepicker, but in this case we need to do a little tricky answer. First you need to create a specific control like this:
Once you have already do this. Exists another post when someone else, I don't rememberer the url for that post, but anyway, the think is you need to overwrite the OnCalendarOpened method just like this:
Notice that we add a last line with the handler for the event DisplayModeChanged. This is really important for the next steps. Ok so the next step is define the DisplayModeChanged
Here there's a couple things, first you need to pick a month, so thats the reason for create the function GetSelectedMonth. The second thing is that method use a class of the WPFToolkit called DateTimeHelper.cs. Ok Until now we just created the functionality for pick up the month. But once you have click on the month calendar, we proceed to show the selected date in month/year format. For accomplish that we just need to create a specific style to our custom control like this:
In my first answer
How to change format (e.g. dd/MMM/yyyy) of DateTimePicker in WPF application
I explained all about with the custom format of the date picker. I hope this help you. please any comment or suggestion, you just let me know, regards!!.
我想提供一种不需要大量代码的替代解决方案。
您可以绑定到 DatePickers 的
CalendarOpened
事件。在这种情况下,您可以将Calendar
的显示模式设置为Decade
,并为Calendar 上的
。然后,在 DisplayModeChanged 事件处理程序中,如果模式为月份,您可以关闭日历,并将DisplayModeChanged
事件添加事件处理程序SelectedDate
设置为当前DisplayDate
。 来说效果很好,这对于我的目的XAML
然后对于背后的代码,
我希望这有帮助!
I want to provide an alternative solution that doesn't require a lot of code.
You can bind to a DatePickers's
CalendarOpened
event. In that event you can set the display mode of theCalendar
toDecade
and add an event handler for theDisplayModeChanged
event on theCalendar
. Then in the DisplayModeChanged event handler you can close the calendar if the mode is month, and also set theSelectedDate
to the currentDisplayDate
. This has worked well for my purposesXAML
Then for the code behind
I hope this helps!
感谢@Fernando García 提供的基础。
我已经为 DatePicker 编写了 DateFormat 和 IsMonthYear 附加属性以启用月/年选择。
IsMonthYear 附加属性将 DatePicker 的 Calendar.DisplayMode 限制为 Year 和 Decade,以防止从 CalendarMode.Month 进行选择。它还将 DatePicker.SelectedDate 的日期部分设置为 1。
DateFormat 附加属性不限于此场景,它还可以与 IsMonthYear 分开使用,为 DatePicker 的显示和输入提供格式字符串。
附加属性的用法示例:
IsMonthYear 附加属性为:
DateFormat 附加属性为:
Thanks to @Fernando García for the basis of this.
I have written DateFormat and IsMonthYear attached properties for DatePicker to enable Month/Year selection.
The IsMonthYear attached property restricts DatePicker's Calendar.DisplayMode to Year and Decade to prevent selection from CalendarMode.Month. It also sets the day portion of DatePicker.SelectedDate to 1.
The DateFormat attached property is not limited to this scenario, it can also be used separate from IsMonthYear to provide a format string for DatePicker's display and input.
Example usage of the attached properties:
The IsMonthYear attached property is:
And the DateFormat attached property is:
如果您可以使用日历控件,则可以
使用此代码隐藏
If you can use the Calendar control instead you could do
with this codebehind