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

3

我正在尝试将一些控件的字体大小(以及其他属性)设置为用户控件级别的字体大小。但是这并没有生效:

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

我做错了什么?谢谢。

4
FontSize 属性从视觉树中的父元素继承。不需要设置它。另外,“RelativeSource Self”指向“TextBlock”,而不是“UserControl”。 - Federico Berasategui
啊,我现在明白我的问题了。我正在捕捉其中一个全局样式。 - t9mike
2个回答

7

我认为这应该是有效的..

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

这会覆盖我的全局样式。谢谢! - t9mike

0

我使用

FontSize="{Binding Parent.Parent.FontSize, RelativeSource={RelativeSource Self}}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接