2012-05-03 49 views
2

我試圖重寫按鈕樣式。 其實我已經做了幾十次,但現在我碰到這個傳來:IsPressed觸發器(按鈕)導致異常

異常和的InnerException: {「屬性不能在觸發空」}

我的代碼:

<Style x:Key="ArrowRightStyle" TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid Height="{Binding ElementName=imgBackground, Path=ActualHeight, Mode=OneWay}" 
         Width="{Binding ElementName=imgBackground, Path=ActualWidth, Mode=OneWay}"> 
        <Image x:Name="imgBackground" Source="{StaticResource RightArrowImageNormal}" Stretch="None"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource RightArrowImageDisabled}"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource RightArrowImageIsPressed}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

</Style> 

,所以它的工作原理:

<Style x:Key="ArrowRightStyle" TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid Height="{Binding ElementName=imgBackground, Path=ActualHeight, Mode=OneWay}" 
         Width="{Binding ElementName=imgBackground, Path=ActualWidth, Mode=OneWay}"> 
        <Image x:Name="imgBackground" Source="{StaticResource RightArrowImageNormal}" Stretch="None"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource RightArrowImageDisabled}"/> 
        </Trigger> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsPressed}" Value="True"> 
         <Setter TargetName="imgBackground" 
           Property="Source" Value="{StaticResource RightArrowImageIsPressed}"/> 
        </DataTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

爲什麼????

回答

2

剛剛從快看,我認爲它無法找到物業IsPressed

添加「按鈕。」應該這樣做。

.... 
    <Trigger Property="Button.IsPressed" Value="True"> 
    <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource.... 

,或者確保您的模板針對一個按鈕(我知道你指定的樣式,但這是不夠的 - 模板將承擔typeof運算(控制)和控制上沒有它IsPressed 。

... 
<Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
      ....the rest of your code 

我認爲

{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsPressed}" 

在運行時解析相對源的類型爲 「按鈕」,因此能夠找到IsPressed ...