Keith G
Keith G

Reputation: 4560

WPF Resource Font Size - Relative to Another Resource

My question is very similar to Wpf custom control template - relative font size ... but I'm trying to set the font size in one resource relative to that of another resource. I implemented the solution posted by Thomas, but I can't figure out how to make the Relative source point to another resource.

            <my:MathConverter x:Key="MathConverter" />

            <Style x:Key="propertyText">
                <Setter Property="Control.Foreground" Value="Gray" />
                <Setter Property="Control.FontSize" Value="12" />
                <Setter Property="Control.Padding" Value="10,2,2,2" />
            </Style>

            <Style x:Key="headerText">
                <!-- I want this to be the same as propertyText +2 -->
                <Setter Property="Control.FontSize" Value="FontSize="{Binding
                 RelativeSource={RelativeSource AncestorType={x:Type Window}},
                 Path=FontSize,
                 Converter={StaticResource MathConverter},
                 ConverterParameter=2}" />
            </Style>

Here is the line I'm having trouble with. I want it to point to propertyText instead:

                 RelativeSource={RelativeSource AncestorType={x:Type Window}},

For completeness, here is the code for the converter :

public class MathConverter : IValueConverter
{
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return (double)value + double.Parse( parameter.ToString() );
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
        return null;
    }
}

Based on Markus Hütter's reply. Here is the XAML for the solution:

           <system:Double x:Key="baseFontSize">12</system:Double>
            <my:MathConverter x:Key="MathConverter" />
            <Style x:Key="propertyText">
                <Setter Property="Control.Foreground" Value="Gray" />
                <Setter Property="Control.FontSize" Value="{StaticResource ResourceKey=baseFontSize}" />
                <Setter Property="Control.Padding" Value="10,2,2,2" />
            </Style>

            <Style x:Key="headerText">
                <!-- I want this to be the same as propertyText +2 -->
               <Setter Property="Control.FontSize" 
                       Value="{Binding Source={StaticResource ResourceKey=baseFontSize},
                       Converter={StaticResource MathConverter},
                       ConverterParameter=2}" />
            </Style>

Upvotes: 0

Views: 3975

Answers (1)

Markus H&#252;tter
Markus H&#252;tter

Reputation: 7906

easiest would be:

create a resource

<system:Double x:Key="propertyTextFontSize">12</system:Double>

and use a StaticReference in the setters both pointing to this resource but the second one with the binding and converter.

Upvotes: 2

Related Questions