答案 0 :(得分:2)
为什么你需要这样做?如果您只想显示月份,那么更简单的方法是在Combox中显示月份列表。
但是我在msdn上找到了适合你的东西。看看https://social.msdn.microsoft.com/Forums/en-US/7bdca56f-719e-44bf-be6d-a9600dfa8f78/wpf-datepicker-for-months-only?forum=wpf
答案 1 :(得分:1)
请尝试以下代码:
DateTime newDateValue = new DateTime(dateTimePicker_month.Value.Year, 1, 1);
dateTimePicker_month.Value = newDateValue;
dateTimePicker_month.Format = DateTimePickerFormat.Custom;
dateTimePicker_month.CustomFormat = "MMM-yyyy";
dateTimePicker_month.ShowUpDown = true;
您必须在2月份添加(1,1),其中有28/29天确定所有月份值。如果您希望查询选择月份。以下是一个示例:
string month = dateTimePicker_month.Value.Month.ToString();
string year = dateTimePicker_month.Value.Year.ToString();
使用以下查询选择月份:
select CAST(date AS DATE) from table where DATEPART(month, date) = '" + month + "' and DATEPART(year,date) = '" + year + "'
答案 2 :(得分:0)
尝试使用格式属性:
dateTimePicker.Format = DateTimePickerFormat.Custom;
dateTimePicker.CustomFormat = "MM";
答案 3 :(得分:0)
使用windows messages approach,您可以检测月份日历控件的显示并强制执行月份视图,并且可以检测视图更改并在逐月视图更改时(选择月份之后)关闭月份日历控件。
最简单的实现方法是覆盖DateTimePicker。
public class MonthPicker : DateTimePicker
{
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NOFITY)
{
var nmhdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
switch (nmhdr.code)
{
case -950:
{
var cal = SendMessage(Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
SendMessage(cal, MCM_SETCURRENTVIEW, IntPtr.Zero, (IntPtr)1);
break;
}
case MCN_VIEWCHANGE:
{
var nmviewchange = (NMVIEWCHANGE)Marshal.PtrToStructure(m.LParam, typeof(NMVIEWCHANGE));
if (nmviewchange.dwOldView == 1 && nmviewchange.dwNewView == 0)
{
SendMessage(Handle, DTM_CLOSEMONTHCAL, IntPtr.Zero, IntPtr.Zero);
}
break;
}
}
}
base.WndProc(ref m);
}
private const int WM_NOFITY = 0x004e;
private const int DTM_CLOSEMONTHCAL = 0x1000 + 13;
private const int DTM_GETMONTHCAL = 0x1000 + 8;
private const int MCM_SETCURRENTVIEW = 0x1000 + 32;
private const int MCN_VIEWCHANGE = -750;
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential)]
struct NMVIEWCHANGE
{
public NMHDR nmhdr;
public uint dwOldView;
public uint dwNewView;
}
}
答案 4 :(得分:0)
由于在我的情况下不起作用,我修改了Orance的答案,将其放在从DateTimePicker继承的类中:
public partial class Startup
{
private readonly IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson();
//Omited for brevity
services.AddModelBinderErrorHandler();
}
//Omited for brevity
}
与VB等效:
protected override void WndProc(ref Message m)
{
if (_MonthSelectStyle)
{
if (m.Msg == 0X204E) // = Win32Messages.WM_REFLECT_NOTIFY
{
var nmhdrI = (NMHDR)(Marshal.PtrToStructure(m.LParam, typeof(NMHDR)));
switch (nmhdrI.code)
{
case -754: // Win32Messages.DTN_DROPDOWN
var cal = SendMessage(m.HWnd, WinApi.Win32Messages.DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
SendMessage(cal, WinApi.Win32Messages.MCM_SETCURRENTVIEW, IntPtr.Zero, (IntPtr)1);
break;
case -759: // Win32Messages.DTN_DATETIMECHANGE
WinApi.SendMessage(Handle, WinApi.Win32Messages.DTM_CLOSEMONTHCAL, IntPtr.Zero, IntPtr.Zero);
break;
}
}
}
base.WndProc(ref m);
}