你能在数据绑定的WPF样式中做“数学”吗?

时间:2008-11-29 18:40:40

标签: wpf data-binding templates styles

我有一个按钮控件样式,我想更改任何数据绑定版本的填充,以调整需要2像素偏移的字形。我将使用SimpleStyles.xaml中的SimpleButton作为示例(...显示为简洁而删除触发器代码的位置):

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

我想要做的是在Padding =“{TemplateBinding Padding}”中添加一些额外的边距。像Padding =“{TemplateBinding Padding} + 2,0,0,0”。

是否有XAML语法?如果没有,在代码(Decorator?)中执行此操作是否有最好的方法?

6 个答案:

答案 0 :(得分:9)

目前XAML不会在Binding语法等中解析表达式。但是,您可以使用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();
    }
}

答案 1 :(得分:3)

Blendables.com上有一个产品名为 Eval Binding ,而 简单装订 就是这样做的现在。 (该产品不是免费的)请查看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}" />

答案 2 :(得分:2)

不,不是在这个版本的XAML中 - 使用值转换器进行数学运算。

答案 3 :(得分:2)

查看this library中的ExpressionConverter

答案 4 :(得分:1)

您可以利用变换做一些简单的数学运算。

查看Charles Petzold很久以前提出的这个伎俩: http://www.charlespetzold.com/blog/2006/04/060223.html

不幸的是,它似乎没有帮助您的特定场景...因为您只想更改Padding的Thickness类型的Left属性...而且这不是您可以单独绑定的依赖项属性。

但是,我觉得有必要添加这个答案,以帮助那些通过谷歌或其他搜索引擎找到他们的人。

答案 5 :(得分:1)

查看MathConverter:http://rachel53461.wordpress.com/2011/08/20/the-math-converter/

在那里你可以发送一个表达式作为converter-parameter,其中@VALUE是你绑定的值:

ConverterParameter=((@VALUE-15)*.2)