2011-03-07 152 views
0

我的问题是非常相似的Wpf custom control template - relative font size ...但我想设置字体大小相对于另一资源的一个资源。我实现了Thomas发布的解决方案,但我无法弄清楚如何将相对源指向另一个资源。WPF资源字体大小 - 相对于其他资源

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

。下面是该解决方案的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> 

回答

1

最简单的是:

创建资源

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

,并使用StaticReference的制定者都指向这个资源,但第二个用绑定和转换器。