[英]Silverlight/WPF Calendar how to get visible dates
我想获得日历中的可见日期,例如下面的图像,我想获得2015年6月28日至2015年8月8日
我得到的所有事件DisplayDateChanged是
已添加日期= {01/07/2015 00:00:00}(7月1日)
RemovedDate = {25/06/2015 00:00:00}(6月25日)
起初,我以为DisplayDateStart和DisplayDateEnd会为我提供此信息,但是我意识到这些属性不是只读的,而是将它们设置为用于其他目的,例如将显示的日期范围。
因此,有什么解决方法或方法可以计算或获得我想要的结果吗?
我通过一些问题以及在日历模板中进行的一些更改得到了答案。
我单击编辑其他模板 / 编辑CalendarDayButtonStyle ,然后添加了以下内容:
<Setter Property="Tag" Value="{Binding Path=Date}" />
private void myCal_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
{
var grid = FindVisualChildByName<Grid>(myCal, "MonthView");
DateTime? dtBegin = null;
DateTime? dtEnd = null;
if (grid != null && grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Any())
{
foreach (var button in grid.Children.OfType<System.Windows.Controls.Primitives.CalendarDayButton>().Cast<System.Windows.Controls.Primitives.CalendarDayButton>())
{
if (dtBegin == null)
dtBegin = Convert.ToDateTime(button.Tag);
else
dtEnd = Convert.ToDateTime(button.Tag);
}
}
}
public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName<T>(child, name);
if (result != null)
return result;
}
}
return null;
}
https://pwnedcode.wordpress.com/2009/04/01/find-a-control-in-a-wpfsilverlight-visual-tree-by-name/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.