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?
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 theTextBox
directly works), while style setters take precedence over 'inherited' values (which is why setting the font size on theUserControl
orContentPresenter
doesn't work - assuming there's indeed a default style at work here). – Coaler