Search code examples
c#wpfwpf-controls

Inside a WPF UserControl, set FontSize on child control to the UserControl's FontSize


I'm trying to set the FontSize -- amoung other things -- on some control's within a UserControl to the UserControl-level FontSize. This is not working:

<UserControl x:Class="TestWpfApplication1.Scratch.UserControlFontSizeProp"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
   <Grid>
       <TextBlock 
        FontSize="{Binding RelativeSource={RelativeSource self},Path=FontSize}">
           Text
       </TextBlock>
   </Grid>
</UserControl>

What am I doing wrong? Thanks.


Solution

  • Think this should work..

    <TextBlock FontSize="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=FontSize}" />