有没有办法将属性绑定到它未绑定时的值

时间:2016-10-31 09:24:33

标签: wpf data-binding

我有一个TextBlock,我希望以环境字体大小的两倍显示。

例如,如果我刚才:

<TextBlock Text="Hello world" />

它将显示在(例如)字体大小14,但我希望它以28的大小显示。绑定到自身的“明显”方法(下面)会导致StackOverflowException,因为你可能会预计有:

<TextBlock Text="Hello world" 
    FontSize="{Binding FontSize, RelativeSource={RelativeSource Self}, Converter={StaticResource ValueDoublingConverter}}" />

这让我使用RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UIElement}ElementName=SomeAncestorControl之类的东西,这两者似乎有些愚蠢(当TextBlock有权访问时,必须离开并寻找另一个控件FontSize本身)。

所以我的问题是:有没有办法将FontSize的{​​{1}}加倍而不必在绑定过程中涉及另一个控件?

2 个答案:

答案 0 :(得分:3)

由于WPF使用矢量图形,因此将文本块的字体大小加倍或仅将整个元素缩放2倍没有区别。

所以你可以设置一个合适的LayoutTransform

<TextBlock Text="Hello, World">
    <TextBlock.LayoutTransform>
        <ScaleTransform ScaleX="2" ScaleY="2"/>
    </TextBlock.LayoutTransform>
</TextBlock>

或更短,使用从stringMatrixTransform的隐式类型转换:

<TextBlock Text="Hello, World" LayoutTransform="2,0,0,2,0,0"/>

答案 1 :(得分:0)

有两种方法:

  1. 创建一个继承TextBlock

    的新控件
    public class MyTextBlock : TextBlock
    {
        static MyTextBlock()
        {
            TextBlock.FontSizeProperty.OverrideMetadata(
                typeof(MyTextBlock),
                new FrameworkPropertyMetadata(
                    TextElement.FontSizeProperty.DefaultMetadata.DefaultValue,
                    TextElement.FontSizeProperty.DefaultMetadata.PropertyChangedCallback,
                    new CoerceValueCallback(CoerceFontSizeProperty))
            );
        }
    
        private static object CoerceFontSizeProperty(DependencyObject d, object baseValue)
        {
            // increase fontsize 2 times    
            return (double)baseValue * 2;
        }
    }
    
  2. 使用Inlines集合的纯XAML,例如; 尝试将FontSize的{​​{1}}更改为24而不是24.我们的Label会在这里完成神奇的工作。

    IValueConverter

    转换器

    <Label FontSize="24" Margin="58,205,325,10"  Width="147">
      <TextBlock HorizontalAlignment="Left"  TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top">
        <TextBlock.Inlines>
            <Run x:Name="Run1" Text="Hi" xmlns:local="clr-namespace:WpfStackOverflow">
                <Run.Style>
                    <Style TargetType="Run">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding FontSize, RelativeSource={RelativeSource AncestorType=TextBlock, Mode=FindAncestor}}" Value="24">
                                <Setter Property="FontSize">
                                        <Setter.Value>
                                            <Binding Path="FontSize" RelativeSource="{RelativeSource AncestorType=TextBlock, Mode=FindAncestor}">
                                                <Binding.Converter>
                                                    <local:DoubleFontSizeConverter/>
                                                </Binding.Converter>
                                            </Binding>
                                        </Setter.Value>
                                    </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Run.Style>
            </Run>
        </TextBlock.Inlines>
      </TextBlock>
    </Label>