在WPF UserControl中,将子控件的FontSize设置为UserControl's FontSize

本文关键字:FontSize UserControl 设置 控件 WPF | 更新日期: 2023-09-27 18:01:38

我试图设置的字体大小-除其他事项-在一些控件的UserControl到UserControl级别的字体大小。

<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>

我做错了什么?谢谢。

在WPF UserControl中,将子控件的FontSize设置为UserControl's FontSize

我想这应该行得通。

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