Workaround for FontSize not getting inherited in Contentpresenter and ContentControl

Trying to make a UserControl that may contain another control. Below is the corresponding code.

<UserControl … … … … > <Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"> … … … <ContentPresenter Content="{Binding SomeContent}"/> … … … </Grid> </UserControl> 

And using this UserControl as shown below -

 <myCtrl:ContainerUserControl FontSize="18pt"> <myCtrl:ContainerUserControl.SomeContent> <Grid> <TextBox Text="Hello World"/> </Grid> </myCtrl:ContainerUserControl.SomeContent> </myCtrl:ContainerUserControl > 

The problem is that FontSize is not inherited in the TextBox. I can install FontSize in a TextBox, but this is not an elegant solution. I tried using ContentControl but no change. Also tried using

 <ContentPresenter TextElement.FontSize="{Binding FontSize}" Content="{Binding SomeContent}"/> 

Does not work. FontSize is not the only thing that bothers me. I may need another property to inherit.

What can be done to solve this problem?

+3
wpf xaml
Nov 11 '15 at 19:29
source share
1 answer

This xaml should work fine. You probably have a default TextBox style that sets the font size. See “Priority of dependency property value” - local values ​​take precedence over style installers (therefore, setting the font size in the TextBox works directly), while stylists take precedence over “inherited” values ​​(therefore, setting the font size in UserControl or ContentPresenter does not work - it is assumed that the default style is really used here).

+2
Nov 13 '15 at 21:12
source share



All Articles