我的问题与Wpf custom control template - relative font size非常相似......但我试图在一个资源中设置字体大小 相对于另一个资源的 。我实现了Thomas发布的解决方案,但我无法弄清楚如何将相对源指向另一个资源。
<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>
这是我遇到麻烦的一条线。我希望它指向propertyText:
RelativeSource={RelativeSource AncestorType={x:Type Window}},
为了完整性,这里是转换器的代码:
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;
}
}
根据MarkusHütter的回复。以下是解决方案的XAML:
<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>
答案 0 :(得分:2)
最简单的是:
创建资源
<system:Double x:Key="propertyTextFontSize">12</system:Double>
并在setter中使用StaticReference指向此资源,但第二个指向此绑定和转换器。