Is it possible to use a converter within a style?

K J picture K J · Dec 18, 2008 · Viewed 16.8k times · Source

Is it possible to use a converter within a style? For instance I am trying to create a styled TextBlock whose text resizes based on the ActualHeight property of the TextBlock. The resizing would be done via a converter.

Answer

Kent Boogaart picture Kent Boogaart · Dec 18, 2008

Yes, this is possible. For example:

<Style TargetType="TextBlock">
    <Setter Property="FontSize">
        <Setter.Value>
            <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}">
                <Binding.Converter>
                    <MyConverter/>
                </Binding.Converter>
            </Binding>
        </Setter.Value>
    </Setter>
</Style>

Depending on your exact scenario, you might also be able to use the more succinct:

<Style TargetType="TextBlock">
    <Setter Property="FontSize" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}"/>
</Style>