简体   繁体   English

DateTimePicker日历默认为上个月/年份

[英]DateTimePicker Calendar defaults to last Month/Year selected

When the DateTime? 什么时候使用DateTime? source of my DateTimePicker control is changed to null and I want to select another date, the calendar drop down defaults to the last month and year selected. DateTimePicker控件的源更改为null,并且我想选择另一个日期,日历下拉列表默认为所选的最后一个月和年份。

I am using the DateTimePicker for DOB so the months and years will vary greatly and I would rather the calendar reset to the current month and year when the data source is changed to null. 我正在使用DOB的DateTimePicker,因此月份和年份会有很大的不同,我宁愿在数据源更改为null时将日历重置为当前月份和年份。 The DateTimePicker from the WPFToolkit does this but I don't want the spinner or the time selection it comes with and cannot find the means to hide those. WPFToolkit中的DateTimePicker可以执行此操作,但是我不希望使用微调器或它附带的时间选择,并且无法找到隐藏这些微调器的方法。

I am using the MVVM design pattern and want to avoid immediate code-behind. 我正在使用MVVM设计模式,并希望避免立即隐藏代码。

XAML from view: XAML从视图:

<DatePicker Height="25" HorizontalAlignment="Left" Margin="53,47,0,0" 
    Name="dpEventDate" VerticalAlignment="Top" Width="115" 
    SelectedDate="{Binding EventItem.BirthDate, Mode=TwoWay, 
    UpdateSourceTrigger=PropertyChanged}" />

C# from view model: (Event is a class I made in the model) 视图模型中的C#:(事件是我在模型中创建的类)

private Event _eventItem;

public event PropertyChangedEventHandler PropertyChanged;
public RelayCommand CreateCommand { get; private set; }

public Event EventItem
{
    get { return _eventItem; }
    set { _eventItem = value; OnPropertyChanged("EventItem"); }
}

public MainViewModel()
{
    EventItem = new Event();
    CreateCommand = new RelayCommand(CreateEvent);
}

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;

    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

If there is anymore detail I can provide to help you help me, just let me know. 如果还有更多细节可以帮助您,请告诉我。

Per Adam's suggestion, here is the code in the view model that has helped me reset the DateTimePicker's Calendar display: 根据亚当的建议,以下是视图模型中的代码,该代码帮助我重置了DateTimePicker的Calendar显示:

private Event _eventItem;
private DateTime? _viewDate;

public event PropertyChangedEventHandler PropertyChanged;
public RelayCommand CreateCommand { get; private set; }

public Event EventItem
{
    get { return _eventItem; }
    set { _eventItem = value; OnPropertyChanged("EventItem"); }
}

// Decoy DateTime property
public DateTime? ViewDate
{
    get { return _viewDate; }
    set
    { 
        _viewDate = value;
        EventItem.EventDate = _viewDate; // Update the source

        OnPropertyChanged("ViewDate");
    }
}

public MainViewModel()
{
    EventItem = new Event();
    CreateCommand = new RelayCommand(CreateEvent);
}

private void CreateEvent()
{
    ViewDate = DateTime.Today; // Important for resetting the calendar display
    // Save input and set EventItem to new event for a clean slate
}

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;

    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }

    // Important if you want your DatePicker control to be empty
    if(name.Equals("ViewDate") && DateTime.Today.Equals(ViewDate))
    {
        ViewDate = null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM