Workaround for FontSize not getting inherited in Contentpresenter and ContentControl
Asked Answered
A

1

5

Trying to make UserControl that can host other control. The following is the relevant code.

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

And using this UserControl as below -

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

Problem is that FontSize is not inherited to the TextBox. I can set FontSize to the TextBox but that is not an elegant solution. I have tried using ContentControl but no change. Also tried to use

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

Doesn’t work as well. FontSize is not the only thing I am worried about. I might need other property to be inheritable as well.

What can be done to solve this problem?

Anishaaniso answered 11/11, 2015 at 19:29 Comment(2)
The given xaml should work fine. You probably have a default TextBox style somewhere that sets the font size. See Dependency Property Value Precedence - local values take precedence over style setters (so setting the font size on the TextBox directly works), while style setters take precedence over 'inherited' values (which is why setting the font size on the UserControl or ContentPresenter doesn't work - assuming there's indeed a default style at work here).Coaler
@PieterWitvoet Thanks. You are right. The problem was due to Mahapps.Metro default control style. Could you post the information as answer so that i can accept it.Anishaaniso
C
5

The given xaml should work fine. You probably have a default TextBox style somewhere that sets the font size. See Dependency Property Value Precedence - local values take precedence over style setters (so setting the font size on the TextBox directly works), while style setters take precedence over 'inherited' values (which is why setting the font size on the UserControl or ContentPresenter doesn't work - assuming there's indeed a default style at work here).

Coaler answered 13/11, 2015 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.