仅在未聚焦时更改背景

时间:2016-04-06 11:15:12

标签: .net wpf xaml

我有这个按钮:

 <Button Content="BUTOON" Style="{StaticResource blackButton}" />

这种风格:

 <Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Border>
                    <Rectangle 
        Margin="2"
        StrokeThickness="1"
        Stroke="#60000000"
        StrokeDashArray="1 2"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- Fill Brushes -->

<SolidColorBrush x:Key="defaultStateBlack" Color="#FF1C1C1C" />
<SolidColorBrush x:Key="defaultStateWhite" Color="#FFFFFF" />
<SolidColorBrush x:Key="selectedStateGreen" Color="#FF22C06C" />


<Style x:Key="blackButton" TargetType="Button">
    <Setter Property="FontFamily" Value="Lato"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="MinHeight" Value="60"/>
    <Setter Property="MaxHeight" Value="60"/>
    <Setter Property="MinWidth" Value="210"/>
    <Setter Property="MaxWidth" Value="210"/>
    <Setter Property="Foreground" Value="#FFFFFF" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border 
      x:Name="Border"  
      CornerRadius="2" 
      BorderThickness="1"
      Background="{StaticResource defaultStateBlack}"
      BorderBrush="{StaticResource defaultStateBlack}">
                    <ContentPresenter 
        Margin="2"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        RecognizesAccessKey="True"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource selectedStateGreen}" />
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource selectedStateGreen}" />
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource defaultStateBlack}" />
                    </Trigger>
                    <!--
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource defaultState}" />
                    </Trigger>
                    -->
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource defaultStateBlack}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource selectedStateGreen}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource selectedStateGreen}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource selectedStateGreen}" />
                        <Setter Property="Foreground" Value="#FFFFFF"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

它是&#34; IsPressed&#34;它周围有一个绿色的边框,并且当#34; IsEnabled&#34;背景变绿。但是我想停止&#34; IsPressed&#34;按钮&#34; IsEnabled&#34;时的事件。当我点击按钮时,#Is; IsEnabled&#34;背景再次变回黑色。我希望它保持绿色,直到例如另一个按钮&#34; IsEnabled&#34;。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您应该使用MultiTrigger。使用MultiTrigger您可以设置多个条件。让我们看看你的情况:

<ControlTemplate.Triggers>
    <MultiTrigger.Conditions>
        <Condition Property="IsPressed" Value="True" />
        <Condition Property="IsEnabled" Value="True" />
    </MultiTrigger.Conditions>
    <MultiTrigger.Setters>
         <!--do what you want-->
         <Setter Property="Background" Value="LightGreen" />
    </MultiTrigger.Setters>

    <Trigger Property="IsKeyboardFocused" Value="true">
        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource selectedStateGreen}" />
        <Setter TargetName="Border" Property="Background" Value="{StaticResource selectedStateGreen}" />
    </Trigger>
   <Trigger Property="IsDefaulted" Value="true">
        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource defaultStateBlack}" />
   </Trigger>
   <Trigger Property="IsEnabled" Value="false">
      <Setter TargetName="Border" Property="Background" Value="{StaticResource selectedStateGreen}" />
      <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource selectedStateGreen}" />
      <Setter Property="Foreground" Value="#FFFFFF"/>
   </Trigger>
</ControlTemplate.Triggers>