2010-11-11 32 views
2

我正在開發一些需要與Silverlight DatePicker類似的功能 - 將顯示一個包含Calendar控件的彈出窗口,並且在用戶單擊日期或使用鍵盤後要選擇日期並按下輸入/空格,彈出窗口應該關閉。檢測在Silverlight日曆控件上單擊一天的時間

我可以顯示日曆很好,但我在計算出用戶點擊一天或按下輸入/空間時不知所措。 SelectedDatesChanged事件沒有給出用戶是否單擊了所選日期的任何指示,或者只是用鍵盤將其忽略。

反光板顯示DatePicker控件使用日曆控件上的內部DayButtonMouseUp事件作弊。

有沒有人知道這個問題的解決方案?

回答

1

不是一個很乾淨的解決方案。此外,它沒有正確考慮BlackoutDates,因爲該按鈕的IsBlackOut屬性也是內部的。我可以手動檢查,在我的點擊事件,但爲我的目的,我不需要支持。

void CalendarControl_Loaded(object sender, RoutedEventArgs e) 
{ 
    var grid = FindVisualChildByName<Grid>(CalendarControl, "MonthView"); 
    // Loaded may be called several times before both the grid and day buttons are created 
    if (grid != null && grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Any()) 
    { 
     // Add our own click event directly to the button 
     foreach (var button in grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Cast<System.Windows.Controls.Primitives.CalendarDayButton>()) 
     { 
      button.Click += new RoutedEventHandler(button_Click); 
     } 
     // We only want to add the event once 
     CalendarControl.Loaded -= new RoutedEventHandler(CalendarControl_Loaded); 
    } 
} 

void button_Click(object sender, RoutedEventArgs e) 
{ 
    var button = (System.Windows.Controls.Primitives.CalendarDayButton)sender; 
    var date = button.DataContext as DateTime?; 
    // The user clicked a date. Close the calendar and do something with it 
} 

FindVisualChildByName從http://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/

2

複製您可以通過DayButtons的ClickMode設置爲 '哈弗' 實現這一目標。 在此之後,您可以輸入MouseLeftButtonDown事件

<sdk:Calendar Name="calendar1" MouseLeftButtonDown="calendar1_MouseLeftButtonDown"> 
     <sdk:Calendar.CalendarDayButtonStyle> 
      <Style TargetType="Primitives:CalendarDayButton"> 
       <Setter Property="ClickMode" Value="Hover"/> 
      </Style> 
     </sdk:Calendar.CalendarDayButtonStyle> 
    </sdk:Calendar> 
相關問題