2013-06-25 80 views
0

如何更改自定義模板中的綁定屬性?更改自定義模板觸發器中的綁定值

按鈕與被綁定到前臺按鈕的路徑填充:

<Button Style="{DynamicResource CustomButtonStyle}"Foreground="White" > 
    <Path Data="PATH_DATA" Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" ></Path> 
</Button> 

這裏是覆蓋模板的自定義樣式:

<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="Background" Value="{x:Null}"/> 
     <Setter Property="BorderBrush" Value="White"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
         <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Foreground" Value="#d5113f"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="True">  
          <Setter Property="Foreground" Value="#d5113f"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

但在觸發

<Setter Property="Foreground" Value="#d5113f"/> 

改變Foregroud什麼都不會做

+0

刪除'前景= 「白」' – WiiMaxx

回答

2

因爲您將Foreground設置爲固定值,所以根據Dependency Property Setting Precedence List,您的樣式觸發器不會覆蓋此值。您需要攜帶Foreground套入Style爲另一個這樣的setter:

<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Foreground" Value="White"/> 
    ... 
</Style> 
+0

謝謝!我想念這個簡單的事情。 –

相關問題