wpf 在代码后面创建样式触发器,2个setter中只有一个在工作

k75qkfdt  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(158)

BorderThicknessProperty被正确地应用,但是ForeGroundProperty没有。我试过交换setter的位置,以为可能只有第一个被应用。我试过其他几个属性,除了BorderThickness没有一个工作。我猜我需要到达Content的Textblock,但是我不知道我怎么做。

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
};

字符串

rqenqsqc

rqenqsqc1#

您为Foreground属性(Foreground = Brushes.Red)分配了本地值。之后可能会触发样式触发器,但触发器中的Foreground setter将被忽略,因为本地值具有更大的权重(它被称为Dependency Property value precedence
要修复该行为,请通过样式设置器设置正常前景值:

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
};

字符串

相关问题