WPF按钮isPressed和isEnabled问题 [英] WPF Button isPressed and isEnabled problem

查看:101
本文介绍了WPF按钮isPressed和isEnabled问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:使用Expression Blend 3

Update: using Expression Blend 3

我正在尝试为IsPressed& IsEnabled(false)属性将触发WPF应用程序中的一类按钮.

I'm trying to style the IsPressed & IsEnabled(false) property triggers for a class of buttons in a WPF application.

这里是带有样式的带按钮的UserControl ...

Here's a UserControl with a Button using the style...

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="Kiosk.ButtonTest"
    x:Name="UserControl">

    <Grid x:Name="LayoutRoot">
        <Button HorizontalAlignment="Left" Style="{DynamicResource BlueButton}" VerticalAlignment="Top" Width="155" Content="Button" Height="52.9"/>
    </Grid>
</UserControl>

这是样式片段...

   <!-- Blue Button -->
   <Style x:Key="BlueButton" TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{DynamicResource BlueGradient3}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/>
            <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient3}"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/>
            <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient1}"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{DynamicResource BlueGradient1}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient2}"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="TextBox.TextAlignment" Value="Center"/>       
    <Setter Property="FontFamily" Value="Trebuchet MS"/>
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="Effect" Value="{DynamicResource KioskStandardDropShadow}" />
</Style>
<LinearGradientBrush x:Key="BlueGradient1" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FF3FA2FD" Offset="0"/>
    <GradientStop Color="#FF014782" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="BlueGradient2" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FF014782" Offset="0"/>
    <GradientStop Color="#FF3FA2FD" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="BlueGradient3" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FF014782" Offset="1"/>
    <GradientStop Color="#FF0B2135" Offset="0"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="DarkGradient1" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FF2A2A2A" Offset="0"/>
    <GradientStop Color="#FF474747" Offset="0.478"/>
    <GradientStop Color="#FF323232" Offset="0.487"/>
    <GradientStop Color="Black" Offset="1"/>
    <GradientStop Color="#FF282828" Offset="0.681"/>
</LinearGradientBrush>
    <!-- Regular Drop Shadow --> 
    <DropShadowEffect x:Key="KioskStandardDropShadow" Opacity="0.6" BlurRadius="10" ShadowDepth="5" Direction="308"/>
<!-- fragment end -->

Default和Mouse over更改工作正常,但是isEnabled false和isPressed true仍显示Button默认颜色.

The Default and Mouse over changes work fine, but isEnabled false and isPressed true still show the Button default colors.

我在做什么错了?

推荐答案

在研究这就是我最终得到的,效果很好.

This is what I ended up with, which works great.

<!-- Blue Button -->
<Style x:Key="BlueButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="border" 
                    BorderThickness="2"
                    Padding="4,2" 
                    BorderBrush="{DynamicResource BlueGradient2}"
                    CornerRadius="5" 
                    Background="{TemplateBinding Background}">
                    <Grid >
                    <ContentPresenter 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center" 
                                Name="content"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{DynamicResource BlueGradient3}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient3}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="{DynamicResource DarkGradient1}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient1}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{DynamicResource BlueGradient1}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource BlueGradient2}"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="TextBox.TextAlignment" Value="Center"/>       
    <Setter Property="FontFamily" Value="Trebuchet MS"/>
    <Setter Property="FontSize" Value="15pt"/>
    <Setter Property="Effect" Value="{DynamicResource KioskStandardDropShadow}" />
</Style>

这篇关于WPF按钮isPressed和isEnabled问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆