2012-05-03 40 views
2

ボタンスタイルをオーバーライドしようとしています。 実際に私はそれを何十回もやったが、今私はこの出くわした:IsPressedトリガー(ボタン)原因例外

例外とのInnerException:{「プロパティがトリガーにnullにすることはできません」}

マイコード:

<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を見つけることができ、したがって、 "Button" などがあり、実行時に相対的なソースの種類を解決することを考えます210

関連する問題