private bool _isFirstLoad = true;//是否第一次加载,避免重复注册事件 public InfoCalendar() { InitializeComponent(); if (_isFirstLoad) { _isFirstLoad = false; this.Loaded += delegate { var template = (ControlTemplate)((Setter)calendar.CalendarItemStyle.Setters[1]).Value; //CalendarItem为calendar模板中的一个元素,名称为PART_CalendarItem,它应用了calendar.CalendarItemStyle var header = (FrameworkElement)calendar.Template.FindName("PART_CalendarItem", calendar); _pan.Invest(this, (FrameworkElement)template.FindName("header", header)); Border tabContainBorder = (Border)tc.Template.FindName("tabContainBorder", tc); tabContainBorder.MinWidth = tc.ActualWidth; tabContainBorder.MinHeight = tc.ActualHeight; _grip.Invest(tabContainBorder, (FrameworkElement)tabContainBorder.FindName("GripPath")); Button tabCloseBtn = (Button)tabContainBorder.FindName("tabCloseBtn"); tabCloseBtn.Click += delegate { Storyboard sb = (Storyboard)((FrameworkElement)(tabContainBorder.Parent)).Resources["sbInfoPanelFadeOut"]; sb.Completed += delegate { calendar.Focus(); tc.SelectedIndex = -1; tabContainBorder.Width = tabContainBorder.MinWidth; tabContainBorder.Height = tabContainBorder.MinHeight; tabContainBorder.Visibility = Visibility.Hidden; }; sb.Begin(); }; tc.SelectedIndex = -1; tc.SelectionChanged += delegate { Storyboard sb = (Storyboard)((FrameworkElement)(tabContainBorder.Parent)).Resources["sbInfoPanelFadeIn"]; if (tabContainBorder.Visibility == Visibility.Hidden && tc.SelectedIndex != -1) { tabContainBorder.Visibility = Visibility.Visible; sb.Begin(); } }; tabContainBorder.DragOver += new DragEventHandler(tabContainBorder_DragOver); this.Unloaded += delegate { //RaiseEvent可在外部触发非本类的事件 tabCloseBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); }; }; } spPad.PreviewMouseLeftButtonDown += spPad_PreviewMouseLeftButtonDown; spPad.PreviewMouseMove += spPad_PreviewMouseMove; }
public ScrollTabControl() { InitializeComponent(); //父控件OnApplyTemplate执行时不代表子控件也OnApplyTemplate //所以将以下逻辑放入Loaded节点中 this.Loaded += delegate { //下句会诡异地得出tp==null的结果(不定期) FrameworkElement tp = (FrameworkElement)tc.Template.FindName("PART_ScrollContentPresenter", tc); _pan.Invest(this, tp); _grip.Invest(this, GripPath); }; }