2011-11-05 43 views
3

嘿傢伙我已經做了一個像下面的自定義時間表,它的工作原理和所有,但現在我試圖添加事件/約會,我無法弄清楚我的生活怎麼辦這個。我希望能夠點擊日曆上的某個按鈕(日期),創建約會/事件,然後將事件標題添加到在正確的gridcell(日期)上創建的txtblock(gridcell)在日曆上。我無法弄清楚如何通過點擊指定的按鈕來訪問特定的網格單元格。我不會爲代碼的寫法感到自豪。我原本想要有一個MVVM,但現在工作。這個問題我不得不每次點擊<或>按鈕來改變月份,新的月份被創建並且舊的被刪除。所以一次只有一個月。請任何幫助表示讚賞。 WPF的爲自定義日曆創建活動/約會WPF

部分:使用ItemsControl的

<ItemsControl.ItemTemplate> 

      <DataTemplate> 

        <StackPanel> 


         <Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Name="btnCalenderDate" Click="btnCalenderDate_Click" Loaded="btnCalenderDate_Loaded" Height="18" FontSize="10" FontWeight="Bold"> 

         </Button> 
         <TextBlock OpacityMask="Black" Name="txtBlockdays" VerticalAlignment="Top" TextWrapping="Wrap"> 

         </TextBlock> 

        </StackPanel> 
       </DataTemplate> 

      </ItemsControl.ItemTemplate> 


     <!-- ItemContainerStyle --> 
     <ItemsControl.ItemContainerStyle> 


      <Style > 

        <Setter Property="Grid.Column" Value="{Binding WeekDay}" /> 
       <Setter Property="Grid.Row" Value="{Binding WeekNo}" /> 

      </Style> 

      </ItemsControl.ItemContainerStyle> 

類,其中壓延而成。

public partial class SchedulePage : Page, INotifyPropertyChanged 
{ 

    public event PropertyChangedEventHandler PropertyChanged; 
    MainWindow _parentForm; 

    Schedule sched = new Schedule(); 
    DateTime date = DateTime.Now; 


    Event newevent; 





    public SchedulePage(MainWindow parentForm) 
    { 
     InitializeComponent(); 

     _parentForm = parentForm; 

     DateTime date = DateTime.Now; //These couple of lines determine the current month to show on startup. 
     _numValue = date.Month; 
     _numYear = date.Year; 
     //int year = date.Year; 
     _nameofmonth = NumValue; 
     _nameofyear = NumYear; 
     lblcurrentdate.Content = date.Month + "/ " + date.Day + "/ " + date.Year; 
     makeCalender(); 
    } 

//The region below are properties that are passed in to makeCalender() when the DateRight and DateLeft buttons 
    // get clicked theses values change. The properties hold the month and the year. Everytime a user 
    // clicks forward or backward to the next month, these values equal to the selected current month & year. 

    // However, Everytime these values change a new calender is made and the old one is cleared(deleted). 
    #region 
    // properties 
    public void NotifyPropertyChanged(String info) 
    { 
     // if (PropertyChanged != null) 
     // { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     //} 
    } 

    private int _numValue; 
    public int NumValue 
    { 
     get { return _numValue; } 
     set 
     { 
      _numValue = value; 

     } 
    } 

    private int _numYear; 
    public int NumYear 
    { 
     get { return _numYear; } 
     set 
     { 
      _numYear = value; 

     } 
    } 


    private int _nameofmonth ; 

    public int NameofMonth 
    { 
     get 
     { 
      return this._nameofmonth; 
     } 

     set 
     { 
      if (value != this._nameofmonth) 
      { 
       this._nameofmonth = value; 
       NotifyPropertyChanged("NameofMonth"); 
      } return; 
     } 
    } 

    private int _nameofyear; 
    public int NameofYear 
    { 
     get 
     { 
      return this._nameofyear; 
     } 

     set 
     { 
      if (value != this._nameofyear) 
      { 
       this._nameofyear = value; 
       NotifyPropertyChanged("NameofYear"); 
      } return; 
     } 
    } 
    #endregion //end of properties 

    //void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e) 
/*  
    { 
     _parentForm.bindings.schedule.Clear(); 
     _nameofmonth = NumValue;//comboMonth.SelectedIndex + 1; 

     _nameofyear = comboYear.SelectedIndex + 2011; 

     makeCalender(); 

    } */ 


    // makeCalender() is how the calender is created and how it determines the gridcell layout of each month. 
    public void makeCalender() { 

     var t = new List<Schedule>(); 
     DateTime curr = DateTime.Now; 
     int[] m = new int[7]; 
     DateTime newcurr = new DateTime(NameofYear, NameofMonth, 1); 

     var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar; 
     var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday); 
     for (int k = 0; k < 7; k++) 
     { 
      for (var i = 1; newcurr.Month == NameofMonth; newcurr = newcurr.AddDays(1)) 
      { 

       var month_week = (newcurr.Day/7); 
       sched.MonthWeek = newcurr.GetWeekOfMonth().ToString(); 
       sched.Month = newcurr.Month.ToString(); 
       sched.Year = newcurr.Year.ToString(); 
       sched.day = newcurr.Day.ToString(); 
       sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString(); 
       sched.dayofweek = newcurr.DayOfWeek.ToString(); 
       t.Add(sched); 

      //Here is where the calender is created. By looping through all the days in the selected month it will find the weeknumber and day of the week --> 
      // that that particular date belongs to and place in the correct gridcell. 
      _parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() }); 

       lblDate.Content = getMonth(newcurr.Month) + " " + newcurr.Year; //getMonth() determines string format of current month displayed. 

      } 
     } 


     DataContext = _parentForm.bindings; 



    } 

    // btnDateRight_Click allows us to navigate forward to the next month, if current month is --> 
    //  december then the current year is incremented and the month starts on january (NumValue = 01) 
    private void btnDateRight_Click(object sender, RoutedEventArgs e) 
    { 
     _parentForm.bindings.schedule.Clear(); 
     if (NumValue >= 1 && NumValue < 12) 
     { 
      NumValue += 1; 

      _nameofmonth = NumValue; 

     } else 
     { 

      NumYear += 1; 
      _nameofyear = NumYear; 
      NumValue = 01; 
      _nameofmonth = NumValue; 
     } 
     makeCalender(); 
    } 

    // btnDateLeft_Click allows us navigate backwards to a previous month 
    // if navigating back and the current month is january, then the year is deincremented the current month is --> 
    // set to December (NumValue = 12) 
    private void btnDateLeft_Click(object sender, RoutedEventArgs e) 
    { 
     _parentForm.bindings.schedule.Clear(); 
     if (NumValue > 1 && NumValue <= 12) 
     { 
      NumValue -= 1; 

      _nameofmonth = NumValue; 


     } 
     else 
     { 
      NumYear -= 1; 
      _nameofyear = NumYear; 
      NumValue = 12; 
      _nameofmonth = NumValue; 

     } 
     makeCalender(); 
    } 

    // btnCalenderDate_Click (as of 11/5/2011) will only return the current month, day, year in messagebox 
    // --> Problem is displaying an event on the textblock(gridcell) of the selected button(date). --> 
    // Example: If I click on the button 5 of november 2011(11/5/2011) I should be able to make an event 
    // and display the title of the event on that pictular gridcell of 11/5/2011. 
    private void btnCalenderDate_Click(object sender, RoutedEventArgs e) 
    { 
     Button b = e.Source as Button; 

     int day = Convert.ToInt32(b.Content); 
     MessageBox.Show(NumValue + "/" + b.Content + "/" + NumYear); 
     //b.Content = "123"; 

     newevent = new Event(NumValue, day, NumYear); 
     newevent.Show(); 



    } 

    //btnCalenderDate_Loaded will determine todays date and highlight the button. 
    private void btnCalenderDate_Loaded(object sender, RoutedEventArgs e) //Sets the current day on the calender 
    { 
     DateTime date = DateTime.Now; 
     Button today = e.Source as Button; 

     if (NumValue == date.Month && (String)today.Content == date.Day.ToString() && NumYear == date.Year) 
     { 
      today.Background = Brushes.Aqua; 
     } 
    } 

回答

0

我不太知道你正在嘗試做的,但你有沒有想過使用DataGrid.HitTest Method它可能是更好的方法。您可以使用鼠標點作爲命中測試參數,它會爲您提供行和列。