9

我有一个按钮控件样式,我想更改任何数据绑定版本的填充,以针对需要 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 语法吗?如果没有,在代码中执行此操作时是否有最佳方法(装饰器?)?

4

6 回答 6

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();
    }
}
于 2008-11-30T18:50:14.530 回答
3

Blendables.com上提供了一个名为Eval Binding的产品,而Simple Binding现在可以做到这一点。(该产品不是免费的)在这里查看白皮书

例如,对于下面的 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}" />
于 2008-11-29T20:54:57.150 回答
2

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

于 2008-11-29T20:16:09.007 回答
2

ExpressionConverter这个库中查看。

于 2008-11-30T18:21:24.620 回答
1

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

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

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

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

于 2011-08-03T23:09:23.227 回答
1

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

在那里,您可以发送一个表达式作为转换器参数,其中 @VALUE 是您绑定到的值:

ConverterParameter=((@VALUE-15)*.2)
于 2013-03-25T16:36:49.613 回答