在代码后置中创建Style Trigger,两个Setter仅一个生效
问题原因与解决方案
核心原因
你遇到的问题是WPF属性值优先级导致的:
- 直接在
pdfButton实例上设置的Foreground = Brushes.Red属于「本地值」,优先级高于Style中Trigger里的Setter,因此Trigger的Foreground修改无法生效。 - 而
BorderThickness没有设置本地值,所以Trigger的Setter能正常覆盖默认值。
解决方案一:将Foreground移到Style的默认Setter中
移除Button实例上的Foreground = Brushes.Red,转而在Style里添加默认的Foreground Setter,这样Trigger的Setter优先级更高,就能正常覆盖:
var foregroundSetter = new Setter() { Property = Button.ForegroundProperty, Value = Brushes.LightGray }; var borderThicknessSetter = new Setter() { Property = Button.BorderThicknessProperty, Value = new Thickness(0) }; var isEnabledTrigger = new Trigger() { Property = Button.IsEnabledProperty, Value = false }; isEnabledTrigger.Setters.Add(foregroundSetter); isEnabledTrigger.Setters.Add(borderThicknessSetter); var style = new Style(typeof(Button)); // 添加默认Foreground的Setter style.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.Red)); // 补充其他默认样式Setter style.Setters.Add(new Setter(Button.BorderBrushProperty, Brushes.Transparent)); style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Transparent)); style.Setters.Add(new Setter(Button.FontSizeProperty, 9.0)); style.Setters.Add(new Setter(Button.FontWeightProperty, FontWeights.SemiBold)); style.Setters.Add(new Setter(Button.PaddingProperty, new Thickness(0))); style.Triggers.Add(isEnabledTrigger); var pdfButton = new Button() { Margin = new Thickness(0, 0, 5, 0), Height = 18, Width = 20, Content = "PDF", ToolTip = new ToolTip() { Content = "Create PDF of training day." }, Command = CreatePdfCommand, CommandParameter = newTab, Style = style };
解决方案二:自定义Button的ControlTemplate
如果需要保留实例上的属性设置,可以自定义ControlTemplate,让内部TextBlock的Foreground直接绑定到Button的Foreground,确保Trigger的修改能传递到显示文本:
// 创建ControlTemplate var buttonTemplate = new ControlTemplate(typeof(Button)); // 构建模板视觉树 var borderFactory = new FrameworkElementFactory(typeof(Border)); borderFactory.SetValue(Border.BorderBrushProperty, Brushes.Transparent); borderFactory.SetValue(Border.BackgroundProperty, Brushes.Transparent); borderFactory.SetValue(Border.BorderThicknessProperty, new Thickness(0)); var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock)); // 绑定Text到Button的Content textBlockFactory.SetBinding(TextBlock.TextProperty, new Binding("Content") { RelativeSource = RelativeSource.TemplatedParent }); // 绑定Foreground到Button的Foreground textBlockFactory.SetBinding(TextBlock.ForegroundProperty, new Binding("Foreground") { RelativeSource = RelativeSource.TemplatedParent }); textBlockFactory.SetValue(TextBlock.FontSizeProperty, 9.0); textBlockFactory.SetValue(TextBlock.FontWeightProperty, FontWeights.SemiBold); textBlockFactory.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center); textBlockFactory.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); borderFactory.AppendChild(textBlockFactory); buttonTemplate.VisualTree = borderFactory; // 构建Style var foregroundSetter = new Setter() { Property = Button.ForegroundProperty, Value = Brushes.LightGray }; var borderThicknessSetter = new Setter() { Property = Button.BorderThicknessProperty, Value = new Thickness(0) }; var isEnabledTrigger = new Trigger() { Property = Button.IsEnabledProperty, Value = false }; isEnabledTrigger.Setters.Add(foregroundSetter); isEnabledTrigger.Setters.Add(borderThicknessSetter); var style = new Style(typeof(Button)); style.Setters.Add(new Setter(Button.TemplateProperty, buttonTemplate)); style.Triggers.Add(isEnabledTrigger); var pdfButton = new Button() { Padding = new Thickness(0, 0, 0, 0), Margin = new Thickness(0, 0, 5, 0), Height = 18, Width = 20, Content = "PDF", FontSize = 9, FontWeight = FontWeights.SemiBold, Foreground = Brushes.Red, BorderBrush = Brushes.Transparent, Background = Brushes.Transparent, ToolTip = new ToolTip() { Content = "Create PDF of training day." }, Command = CreatePdfCommand, CommandParameter = newTab, Style = style };
内容的提问来源于stack exchange,提问作者Steve S
相关产品推荐
相关产品推荐

