kodu
kodu

Reputation: 2386

Styles versus property inheritance

I'm dealing with a WPF application where element-typed styles are defined for most basic controls (textbox and such like). I now have a control, in a control, in a control ... basically I have no way of directly altering the textbox properties, so I set the property (fontsize) in my control in the hope that this property would propagate to all children of the control. Unfortunately the element-typed style in taken in preference to my control property.

Is there any way of overriding these element-typed styles?

Upvotes: 1

Views: 207

Answers (2)

Klaus78
Klaus78

Reputation: 11906

If I understand well you set the FontSize for a TextBox in a Style (e.g. FontSize=15). You also have a control that contains some Textbox children.

When you set Control FontSize=10, then the TextBox children have FontSize 15 while you expect it to be 10.

Is this your problem? If yes this is what happens:

FontSize is a Dependency Property. As a such, its value can be given by many different providers that are listed (simplified list taken from Dependency Property Setting Precedence List msdn) from higher to lower priority in the following

  1. Property system coercion.
  2. Active animations, or animations with a Hold behavior.
  3. Local value.
  4. TemplatedParent template properties.
  5. Implicit style. Applies only to the Style property.
  6. Style triggers.
  7. Template triggers.
  8. Style setters.
  9. Default (theme) style. Within a default style, the following order of precedence applies:
  10. Inheritance.
  11. Default value from dependency property metadata.

In your specific case, the TextBox FontSize will be set according to the following sources:

  • FontSize=15 from Style (priority 8)
  • FontSize=10 from Control FontSize (priority 10)

The value from FontSize will win because it has higher priority.

In conclusion, you can override the FontSize given by Style by using a provider with higher priority according to the setting precedence list.

I hope this helps

Upvotes: 1

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

How are the element specific properties set? E.g. for TextBox is FontSize = 10 and for combobox is it 11? Or is it 10 i.e. standard sized across all elements? If so why did you set the same value across element styles if it was same?

If its different across elements then what do you expect when you set a control specific fontsize? Should it overrride element specific fontsize? If so why did you set element specific fontside in first place? SHould your control (or App rather) define a fontsize that would cascade down to the smallest element in the visual tree (if same foint ies expected everywhere?). In such case I would suggest you to get rid of the element specific fontsizes.

Upvotes: 0

Related Questions