2008-11-29 6 views
8

버튼 컨트롤 스타일이 있고 데이터 바인딩 된 버전이 2 픽셀 오프셋이 필요한 그림 문자를 조정하기 위해 무엇이든간에 패딩을 변경하고 싶습니다.WPF 내에서 "math"할 수 있습니까? 데이터 바인딩 된 스타일

<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이다 : 나는 (... 트리거 코드가 간결하게 제거 된 곳이다) 예를 들어 SimpleStyles.xaml에서 SimpleButton을 사용합니다 심}". Padding = "{TemplateBinding Padding} + 2,0,0,0"과 같은 것입니다.

거기에 XAML 구문이 있습니까? 그렇지 않다면 코드 (Decorator?)에서이를 수행 할 때 가장 좋은 방법이 있습니까?

답변

8

현재 XAML은 당신이 자신을 도와하는 IValueConverter 또는 IMultiValueConverter을 사용할 수 있습니다, 그러나 등의 구문을 바인딩에 식을 구문 분석하지 않습니다 :

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

당신의 예를 제공 할 수 있습니다 가치 계산기를 사용하여이 수학을 해보십시오 - 오용과 같은 것처럼 보입니다. –

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}" /> 
2

체크 아웃 this library에서 ExpressionConverter 같이 할 수 있습니다.

1

변환을 이용하면 간단한 수학을 할 수 있습니다.

체크 아웃 찰스 페 졸드 오래 전에 해낸이 트릭 : http://www.charlespetzold.com/blog/2006/04/060223.html

불행하게도, 당신 만의 왼쪽 속성을 변경하려면 때문에 ... 특정 시나리오에 도움하지 않는 것을 안쪽 여백에 대한 두께 유형 ...이 속성은 사용자가 단독으로 바인딩 할 수있는 종속성 속성이 아닙니다.

그러나 Google이나 다른 검색 엔진을 통해 다른 사람들에게 도움이되는 경우이 답변을 추가해야한다고 느꼈습니다.

관련 문제