I am creating a custom WPF control that let’s say for simplicity sake has a vertical stack panel with a ‘title’ TextBlock, followed by a ContentPresenter. I want the font size for the ‘title’ to be 5 Points LARGER than the size used in the content, which is inherited by whatever container the user places this control in.
How can I specify a font size in the control template for the header element using a relative value without exposing a property like ‘TitleFontSize’ to the user? I want do ‘add 5’.
I tried using a ScaleTransform on the header text block with mixed results (the text block scaled fine but the orientation was modified – I had the text right-justified and it moved ‘off the control’ area when scaled). Also, I am not sure if scale transform would be approprite here.
I did it with an IValueConverter as follows:
Created a class FontSizeConverter that derives from IValueConverter. The Convert method adds 10 to the value, and the ConvertBack method subtracts 10.
Next, I declaried an instance of this class in the XAML template for the control:
And Finnaly, the FontSize binding uses this converter applied to the inherited FontSize property:
This works. But I still do not know if this is the correct answer. Let me know if there is a better way, or if this is appropriate.