我正在开发需要与Silverlight DatePicker类似功能的东西 - 将显示包含Calendar控件的弹出窗口,并在用户单击日期后,或使用键盘选择日期并按Enter / space,弹出窗口应该关闭。
我可以很好地显示日历,但是当用户点击一天或按下输入/空格时,我无法搞清楚。 SelectedDatesChanged
事件未显示用户是否点击了所选日期,或者只是用键盘将其翻过来。
Reflector显示DatePicker控件在日历控件上使用内部DayButtonMouseUp
事件作弊。
有没有人知道这个问题的解决方案?
答案 0 :(得分:2)
您可以通过将DayButtons的ClickMode设置为“Hover”来实现此目的。 在此之后,你可以看到MouseLeftButtonDown-event
<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>
答案 1 :(得分: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/
复制的