2008-11-29 68 views
8

我有一個按鈕控制樣式,並且我想要更改數據綁定版本針對需要2像素偏移量的字形調整的填充。我將使用的SimpleButton從SimpleStyles.xaml爲例(...表示,爲了簡明去除觸發代碼):您可以在數據綁定的WPF樣式中執行「數學」

<Style x:Key="SimpleButton" TargetType="{x:Type Button}" BasedOn="{x:Null}"> 
    <Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{DynamicResource NormalBrush}"/> 
    <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <!-- We use Grid as a root because it is easy to add more elements to customize the button --> 
      <Grid x:Name="Grid"> 
      <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/> 

       <!-- Content Presenter is where the text content etc is placed by the control. The bindings are useful so that the control can be parameterized without editing the template --> 
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/> 
      </Grid> 
    ... 
    </Setter.Value>      
    </Setter> 
</Style> 

我想要做的就是添加一些額外的保證金,其中填充=「{TemplateBinding填充}」。像Padding =「{TemplateBinding Padding} + 2,0,0,0」。

是否有XAML語法?如果沒有,在代碼(Decorator?)中做這件事情時是否有最佳方法?

回答

8

目前XAML並不解析表達式綁定語法等。但是,你可以使用一個IValueConverterIMultiValueConverter,以幫助自己走出:

XAML:

<Setter.Value> 
    <ControlTemplate TargetType="{x:Type Button}"> 
     <Grid x:Name="Grid"> 
     <Grid.Resources> 
      <local:ThicknessAdditionConverter x:Key="AdditiveThickness" /> 
     </Grid.Resources> 
     <Border x:Name="Border"> 
      <Border.Padding> 
       <Binding Path="Padding" RelativeSource="{RelativeSource TemplatedParent}" 
          Converter="{StaticResource AdditiveThickness}"> 
        <Binding.ConverterParameter> 
         <Thickness>2,0,0,0</Thickness> 
        </Binding.ConverterParameter> 
       </Binding> 
      </Border.Padding> 
     </Border> 
... 
</Setter.Value> 

的IValueConverter後面的代碼:

public class ThicknessAdditionConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null) return new Thickness(0, 0, 0, 0); 
     if (!(value is Thickness)) throw new ArgumentException("Value not a thickness", "value"); 
     if (!(parameter is Thickness)) throw new ArgumentException("Parameter not a thickness", "parameter"); 

     var thickness = new Thickness(0, 0, 0, 0); 
     var t1 = (Thickness)value; 
     var t2 = (Thickness)parameter; 

     thickness.Left = t1.Left + t2.Left; 
     thickness.Top = t1.Top + t2.Top; 
     thickness.Right = t1.Right + t2.Right; 
     thickness.Bottom = t1.Bottom + t2.Bottom; 

     return thickness; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
2

不,不在此版本的XAML中 - 使用價值轉換器來計算您的數學。

+0

你能提供的示例使用價值轉換器來做這個數學 - 它似乎更像是對我的濫用。 – 2008-11-30 04:25:58

3

有可在Blendables.com一款名爲評估和演示綁定簡單的綁定現在做到這一點。 (該產品不是免費的)在這裏查看whitepaper

例如對於波紋管XAML代碼,您需要一個轉換器來執行操作。

<Ellipse Fill="Blue" Height="50" 
    Width="{Binding RelativeSource={RelativeSource Self}, 
     Path=Height, Converter={StaticResource MyConverter}}" /> 

但隨着EvalBinding就可以像波紋管

<Ellipse Fill="Blue" Height="50" 
    Width="{blendables:EvalBinding [{Self}.Height]/2}" /> 
1

你可以通過利用變換來做一些簡單的數學運算。

退房這一招,查爾斯Petzold的想出了一個很久以前的事: http://www.charlespetzold.com/blog/2006/04/060223.html

不幸的是,它似乎並沒有幫助您的特定情況......因爲只需要改變Left屬性Padding的厚度類型...並且這不是您可以單獨綁定的依賴項屬性。

但是,我感到不得不在這種情況下增加這個答案,以幫助其他人通過Google或其他搜索引擎找到他們的方式。