-1

我想从对象发送者那里获取选定的日期并将该日期传递给其他方法。那么,如何从发件人对象中获取日期。

我的 C# 代码;

  public partial class MonthView : MetroWindow
{
    AppController a=new AppController();


    public MonthView()
    {
        InitializeComponent();
        calMain.DisplayDate = DateTime.Today;
    }

    public void calItemSelectedDate(object sender, SelectionChangedEventArgs e)
    {
        //sender = calMain.SelectedDate;   
        //DateTime d = (DateTime)sender;
        //a.fetchAndPopulateForDate(d);            <------I want code here.
        //DayView Activity = new DayView();
        //Activity.Show();
        //this.Close();

    }

 }
4

1 回答 1

3

sender始终是引发事件的控件。在这种情况下,它是Calendar. 因此,请按照以下方式进行转换:

public void calItemSelectedDate(object sender, SelectionChangedEventArgs e)
{
    Calendar cal = (Calendar)sender; 
    DateTime d = cal.SelectedDate;
    a.fetchAndPopulateForDate(d); <------I want code here.
    // ...
}

(但是,由于您也可以通过calMain.SelectedDate我直接访问它,所以我不太了解问题所在)

于 2013-05-31T07:33:38.747 回答