【问题标题】:Custom button border style does not change on IsPressed event自定义按钮边框样式不会在 IsPressed 事件上更改
【发布时间】:2020-06-06 21:54:34
【问题描述】:

我正在尝试在 WPF 中重新创建 Valve VGUI 界面样式。到目前为止,我可以为按钮分配边框,但按下按钮时边框样式不会改变。

这是我的 XAML 代码:

 <Window.Resources>
    <Style x:Key="BorderLight_Style" TargetType="Border">
        <Setter Property="BorderBrush" Value="#889180" />
        <Setter Property="BorderThickness" Value="1,1,0,0" />
    </Style>
    <Style x:Key="BorderShadow_Style" TargetType="Border">
        <Setter Property="BorderBrush" Value="#FF282E22" />
        <Setter Property="BorderThickness" Value="0,0,1,1" />
    </Style>
    <Style x:Key="BorderLight_Style_pressed" TargetType="Border">
        <Setter Property="BorderBrush" Value="#FF282E22" />
        <Setter Property="BorderThickness" Value="1,1,0,0" />
    </Style>
    <Style x:Key="BorderShadow_Style_pressed" TargetType="Border">
        <Setter Property="BorderBrush" Value="#889180" />
        <Setter Property="BorderThickness" Value="0,0,1,1" />
    </Style>
    <Style TargetType="Grid">
        <Setter Property="Background" Value="#FF4C5844" />
    </Style>
    <Style TargetType="Button">
        <Setter Property="Background" Value="#FF4C5844" />
        <Setter Property="Foreground" Value="#D8DED3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" Style="{StaticResource BorderShadow_Style}">
                        <Border x:Name="border2" Style="{StaticResource BorderLight_Style}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Style" Value="{StaticResource BorderShadow_Style_pressed}" />
                            <Setter TargetName="border2" Property="Style" Value="{StaticResource BorderLight_Style_pressed}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

我这样做有什么问题吗?谢谢!

【问题讨论】:

    标签: c# wpf xaml events triggers


    【解决方案1】:

    控件需要有一个已定义的Background 属性才能参与命中测试,因此例如允许在Button 上设置IsPressed 属性。

    将您的内部BorderBackground 属性设置为任何有效值 - 例如Transparent - 以修复您的Trigger


    注意:如果您按TAB 选择您的按钮并按空格键,即使您没有设置任何背景,您也会看到您的Trigger 工作,因为IsPressedalso reacts to the space bar.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多