void lbHeader_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { e.Handled = true; if (this.IsSelected) { return; } TonyTabControl parent = this.Parent as TonyTabControl; if (parent != null && parent.Items.Count > 0) { foreach (var item in parent.Items) { TonyTabItem tonyTabItem = item as TonyTabItem; if (item == null) { throw new Exception("非TonyTabItem"); } //OldItem方面待后续处理,现在只能传入当前选中的Item //if (tonyTabItem.IsSelected) // oldSelectedItem = tonyTabItem;//获取以前被选中的Item if (tonyTabItem != null) { if (tonyTabItem == this) { tonyTabItem.IsSelected = true; } else { tonyTabItem.IsSelected = false; } } } } }
private static void OnSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TonyTabItem tonyTabItem = d as TonyTabItem; //激发SelectionChanged事件,然后TabControl中处理 if (tonyTabItem.ItemSelected != null && (bool)e.NewValue) { tonyTabItem.ItemSelected(new SelectionChangedCancelEventArgs(tonyTabItem.oldSelectedItem, tonyTabItem)); } }
public SelectionChangedCancelEventArgs(TonyTabItem oldItem, TonyTabItem newItem) { OldSelectedItem = oldItem; NewSelectedItem = newItem; }
private static void OnHeaderSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TonyTabItem tonyTabItem = d as TonyTabItem; TonyTabControl tonyTabControl = tonyTabItem.Parent as TonyTabControl; if (tonyTabControl == null) { return; } if (tonyTabItem.mainGrid == null) { return; } if (tonyTabItem.lbHeader == null) { return; } //处理leftMargin和HeaderHeight List <TonyTabItem> items = new List <TonyTabItem>(); foreach (var item in tonyTabControl.Items) { items.Add(item as TonyTabItem); } //找出最大的HeaderHeight double headerHeightMax = (from i in items select i.HeaderHeight).Max(); //都设置为最大的headerHeightMax items.ForEach(x => x.HeaderHeight = headerHeightMax); //处理leftMargin:TonyTabItem中的Leftmargin是Header和左边的距离,现在改为Header与Header的距离 double leftMargin = 0; foreach (TonyTabItem item in items) { leftMargin += item.HeaderLeftMargin; //item.HeaderLeftMarginEx = leftMargin; if (item == tonyTabItem) { break; } leftMargin += item.HeaderWidth; } //获取LeftMargin:Header距离最左边的距离 tonyTabItem.HeaderLeftMarginEx = leftMargin; //设置Header的Margin tonyTabItem.lbHeader.Margin = new Thickness(leftMargin, 0, 0, 0); //处理剪切 double left = tonyTabItem.HeaderLeftMarginEx; double h = tonyTabItem.HeaderHeight; double w = tonyTabItem.HeaderWidth; double ah = tonyTabControl.ActualHeight; double aw = tonyTabControl.ActualWidth; //Header 有4个点,ContentPresenter有各点,按点裁剪 Point p1 = new Point(left, 0); Point p2 = new Point(left + w, 0); Point p3 = new Point(left + w, h); Point p4 = new Point(aw, h); Point p5 = new Point(aw, ah); Point p6 = new Point(0, ah); Point p7 = new Point(0, h); Point p8 = new Point(left, h); StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open()) { ctx.BeginFigure(p1, true, true); ctx.LineTo(p2, true, true); ctx.LineTo(p3, true, true); ctx.LineTo(p4, true, true); ctx.LineTo(p5, true, true); ctx.LineTo(p6, true, true); ctx.LineTo(p7, true, true); ctx.LineTo(p8, true, true); } tonyTabItem.mainGrid.Clip = geometry;//按照Header进行裁剪 }