Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf i按下触发器(按钮)导致异常_Wpf_Templates_Button_Triggers_Styles - Fatal编程技术网

Wpf i按下触发器(按钮)导致异常

Wpf i按下触发器(按钮)导致异常,wpf,templates,button,triggers,styles,Wpf,Templates,Button,Triggers,Styles,我正在尝试覆盖按钮样式。 事实上,我已经做了几十次了,但现在我发现: 异常和内部异常: {“触发器中的属性不能为null。”} 我的代码: <Style x:Key="ArrowRightStyle" TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> <Setter Propert

我正在尝试覆盖按钮样式。 事实上,我已经做了几十次了,但现在我发现:

异常和内部异常: {“触发器中的属性不能为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>


为什么???

仅从快速查看,我认为它找不到显示的属性

添加“按钮”。应该这样做

....
  <Trigger Property="Button.IsPressed" Value="True">
     <Setter TargetName="imgBackground" Property="Source" Value="{StaticResource....
在运行时将相对源的类型解析为“Button”,因此能够找到IsPressed

...
<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
            ....the rest of your code
{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsPressed}"