BorderThicknessProperty 已正确应用,但 ForeGroundProperty 未正确应用。我尝试过交换二传手的位置,认为可能只有第一个被应用。我尝试了其他几个属性,除了 BorderThickness 之外,没有一个可以工作。我猜我需要访问内容的文本块,但我不知道如何做到这一点。
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
//Setters = { foregroundSetter, borderThicknessSetter }
};
isEnabledTrigger.Setters.Add(foregroundSetter);
isEnabledTrigger.Setters.Add(borderThicknessSetter);
var style = new Style();
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
};
您为 Foreground 属性分配了本地值 (
Foreground = Brushes.Red
)。之后,可能会触发样式触发器,但触发器中的前景 setter 将被忽略,因为本地值具有更大的权重(称为 依赖属性值优先级)
要修复该行为,请通过样式设置器设置正常的前景值:
var foregroundNormalSetter = new Setter()
{
Property = Button.ForegroundProperty,
Value = Brushes.Red
};
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
//Setters = { foregroundSetter, borderThicknessSetter }
};
isEnabledTrigger.Setters.Add(foregroundSetter);
isEnabledTrigger.Setters.Add(borderThicknessSetter);
var style = new Style();
style.Setters.Add(foregroundNormalSetter);
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
};