WPF TabItem样式拧紧控制位置

时间:2017-09-19 17:34:19

标签: c# wpf

我创建了一个新的TabItem样式,它看起来像我想要的样子。但现在它搞砸了TabItem上放置的所有控件的位置。它将它们从TabItem的左上区域移动到右下区域。任何帮助将受到高度赞赏。 (是的,我确信我需要C#中的样式而不是XAML)

var functionReturnValue = new Style();

ControlTemplate L_NewControlTemplate = new ControlTemplate(typeof(TabItem));
FrameworkElementFactory L_ElemFactory = new FrameworkElementFactory(typeof(Border));
L_ElemFactory.SetBinding(Border.FocusVisualStyleProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("FocusVisualStyle") });
L_ElemFactory.SetBinding(Border.BackgroundProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("Background") });
L_ElemFactory.SetBinding(Border.BorderBrushProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("BorderBrush") });
L_ElemFactory.SetBinding(Border.BorderThicknessProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("BorderThickness") });
//L_ElemFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(0,12,0,0));
L_ElemFactory.SetValue(Border.SnapsToDevicePixelsProperty, true);
FrameworkElementFactory L_ContentPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
L_ContentPresenter.SetValue(ContentPresenter.ContentSourceProperty, "Header");
L_ContentPresenter.SetBinding(ContentPresenter.HorizontalAlignmentProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("HorizontalContentAlignment") });
L_ContentPresenter.SetBinding(ContentPresenter.VerticalAlignmentProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("VerticalContentAlignment") });
L_ContentPresenter.SetBinding(ContentPresenter.SnapsToDevicePixelsProperty, new System.Windows.Data.Binding { RelativeSource = System.Windows.Data.RelativeSource.TemplatedParent, Path = new PropertyPath("SnapsToDevicePixels") });
L_ElemFactory.AppendChild(L_ContentPresenter);
L_NewControlTemplate.VisualTree = L_ElemFactory;

Trigger NewTrigger;
//Trigger IsEnabled = true
NewTrigger = new Trigger { Property = TabItem.IsEnabledProperty, Value = true };
NewTrigger.Setters.Add(new Setter(TabItem.ForegroundProperty, F_Foreround));
NewTrigger.Setters.Add(new Setter(TabItem.BackgroundProperty, F_Background));
L_NewControlTemplate.Triggers.Add(NewTrigger);


//Trigger MouseOver
NewTrigger = new Trigger { Property = TabItem.IsMouseOverProperty, Value = true };
NewTrigger.Setters.Add(new Setter(TabItem.ForegroundProperty, F_Foreround));
NewTrigger.Setters.Add(new Setter(TabItem.BackgroundProperty, F_MouseOver));
L_NewControlTemplate.Triggers.Add(NewTrigger);

//Trigger Selected
NewTrigger = new Trigger { Property = TabItem.IsSelectedProperty, Value = true };
NewTrigger.Setters.Add(new Setter(TabItem.ForegroundProperty, F_Foreround));
NewTrigger.Setters.Add(new Setter(TabItem.BackgroundProperty, F_Background));
L_NewControlTemplate.Triggers.Add(NewTrigger);

//Trigger Selected
NewTrigger = new Trigger { Property = TabItem.IsSelectedProperty, Value = false };
NewTrigger.Setters.Add(new Setter(TabItem.BackgroundProperty, L_IsDeActive));
L_NewControlTemplate.Triggers.Add(NewTrigger);

functionReturnValue.Setters.Add(new Setter(TabItem.FocusVisualStyleProperty, null));
functionReturnValue.Setters.Add(new Setter(TabItem.BackgroundProperty, F_Background));
functionReturnValue.Setters.Add(new Setter(TabItem.BorderBrushProperty, Brushes.Green));
functionReturnValue.Setters.Add(new Setter(TabItem.BorderThicknessProperty, new Thickness(0)));
functionReturnValue.Setters.Add(new Setter(TabItem.ForegroundProperty, F_Foreround));
functionReturnValue.Setters.Add(new Setter(TabItem.HorizontalContentAlignmentProperty, System.Windows.HorizontalAlignment.Center));
functionReturnValue.Setters.Add(new Setter(TabItem.VerticalContentAlignmentProperty, System.Windows.VerticalAlignment.Center));
functionReturnValue.Setters.Add(new Setter(TabItem.PaddingProperty, new Thickness(0)));
functionReturnValue.Setters.Add(new Setter(TabItem.MarginProperty, new Thickness(-4, -4, 0, 0)));
functionReturnValue.Setters.Add(new Setter(TabItem.TemplateProperty, L_NewControlTemplate));
return functionReturnValue;

1 个答案:

答案 0 :(得分:0)

所以事实证明你的第一个问题是这两行:

functionReturnValue.Setters.Add(new Setter(TabItem.HorizontalContentAlignmentProperty, System.Windows.HorizontalAlignment.Center));
functionReturnValue.Setters.Add(new Setter(TabItem.VerticalContentAlignmentProperty, System.Windows.VerticalAlignment.Center));

省略那些;他们会对标签项内容起作用,而不是在标题上。

要重新设置标题,您有很多选择。

这将为您的内容演示者提供20个单位的保证金:

L_ContentPresenter.SetValue(ContentPresenter.MarginProperty, new Thickness(20));
20非常随意;当然你可以使用自己的价值观。这将为其提供20个单位边距和10个单位上/下边距:

L_ContentPresenter.SetValue(ContentPresenter.MarginProperty, new Thickness(20, 10, 20, 10));

您还可以在Border周围的标题控件模板中放置ContentPresenter或其他内容,并对包含控件的填充或边距执行某些操作。这种事情在XAML中更容易使用,因此我不会在C#中做到这一点。

您可以设置TabItem样式集HeaderTemplate,这是DataTemplate

var headerTemplate = new DataTemplate();
var label = new FrameworkElementFactory(typeof(Label));

label.SetBinding(Label.ContentProperty, new Binding());

label.SetValue(Label.MarginProperty, new Thickness(16, 12, 16, 12));

headerTemplate.VisualTree = label;

functionReturnValue.Setters.Add(new Setter(TabItem.HeaderTemplateProperty, headerTemplate));

最后,我想重申一下,无论你在运行时需要做什么,你都可以在XAML中比在C#中更轻松,更可维护。