工作笔记
private static ChildItem FindVisualChildItem<ChildItem>(DependencyObject obj, string name) where ChildItem : FrameworkElement
{
if (null != obj)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is ChildItem && (child as ChildItem).Name.Equals(name))
{
return (ChildItem)child;
}
else
{
ChildItem childOfChild = FindVisualChildItem<ChildItem>(child, name);
if (childOfChild != null && (childOfChild as ChildItem).Name.Equals(name))
{
return childOfChild;
}
}
}
}
return null;
}
void calendar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine("calendar MouseLeftButtonDown");
e.Handled = true;
}
// Find the Popup in template
Popup popup = FindVisualChildItem<Popup>(sender as DatePicker, "PART_Popup");
var calendar = (Calendar)popup.Child;
calendar.MouseLeftButtonDown += new MouseButtonEventHandler(calendar_MouseLeftButtonDown);
//Get previouts button
string previousBtnName = "PART_PreviousButton";
var previousBtn = FindVisualChildItem<Button>(calendar, previousBtnName);
if (previousBtn != null)
{
previousBtn.PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(previousBtn_PreviewMouseLeftButtonDown);
previousBtn.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(previousBtn_PreviewMouseLeftButtonDown);
}