2011-03-07 88 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的制定者都指向這個資源,但第二個用綁定和轉換器。