Quentamia
Quentamia

Reputation: 3292

Applied style in WPF ignores properties

Here's the conundrum. In two different places in my application, I use a border with the exact same appearance. In an never-ending fight against code bloat and unmanageable code, I want to define the border's appearance in a style to use when I create the border. Strangely, several of the properties I set are being ignored.

Here's the code I use to create the style. Simple enough.

Style borderStyle = new Style(typeof(Border));

borderStyle.Setters.Add(new Setter(Border.BorderBrushProperty, Brushes.Black));
borderStyle.Setters.Add(new Setter(Border.BorderThicknessProperty, new Thickness(4)));
borderStyle.Setters.Add(new Setter(Border.OpacityProperty, 1.0));

return borderStyle;

But when I set the style, the opacity property is left at its original value of 0.7. I have also tried setting the background of the border to a brush I created. It too is ignored.

Thanks for any insights you may have.

Upvotes: 0

Views: 349

Answers (1)

rrhartjr
rrhartjr

Reputation: 2114

You mention a default setting of 0.7. Properties explicitly set take precedence over Style properties. Remove the explicitly set property and your Style should be used.

Same goes for the Background Brush, if it's explicitly set on the Element, the Style values will be overridden.

See here for the precedence list: http://msdn.microsoft.com/en-us/library/ms743230.aspx#listing

Upvotes: 1

Related Questions