2012-05-03 50 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 ...