wpf - IsPressed 触发器(按钮)导致异常

标签 wpf templates button triggers styles

我正在尝试覆盖按钮样式。 其实我已经做过几十次了,但现在我遇到了这个:

异常和内部异常: {“触发器中的属性不能为空。”}

我的代码:

<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>

为什么???

最佳答案

从快速查看来看,我认为它找不到 IsPressed 属性

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

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

或者确保您的模板针对一个按钮(我知道您在样式上指定了该按钮,但这还不够 - 模板将假定 typeof(Control) 并且控件上没有 IsPressed。

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

我认为

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

在运行时将相对源的类型解析为“Button”,因此能够找到 IsPressed...

关于wpf - IsPressed 触发器(按钮)导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438181/

相关文章:

java - 带图像的 android 按钮

c# - 按钮 PlayerPrefs

c# - 图像到内存慢

c# - 将 DataTable 绑定(bind)到 DataGrid 显示空行

c# - 如何从代码隐藏发送多个击键

c++ - 如果可用,使用模板参数的实现,否则默认

wpf - 当依赖属性改变时运行动画

wpf - 将 TreeView 的布局更改为看起来像多个 ListBox

css - Drupal View : Exposing Individual Fields on Output for Styling

Android按钮文字外观