Raikoug
Raikoug

Reputation: 377

wpf button after click behavior

I'm doing a wpf program, and I did a custom style button:

<Style x:Key="ButtonMio" TargetType="{x:Type Button}">
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="border" Background="{TemplateBinding Background}">
                        <ContentPresenter Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  Margin="{TemplateBinding Padding}"
                                  RecognizesAccessKey="True"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">

                            <Setter Property="Background" Value="#FF3EC8C6" />
                            <Setter Property="Foreground" Value="Transparent"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">

                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="Transparent"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">

                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="Transparent"/>
                            <Setter Property="BorderBrush" Value="Transparent"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Well, the button ha an image as content (so it's smaller than the real button). Since the background is transparent, i wrote this in mouse over trigger:

<Setter Property="Background" Value="#FF3EC8C6" />

And it works. But only before the click. Afterclick there is no more background. Even clicking other buttons and making the first one to lose the focus (i think) the background still doesn't appears. I tiried changing the "IsPressed" and "IsFocused", but in first case, the background doesn't disappear afterclick, and in second, nothing couse I don't use tab to focus elements.

Really sorry for my english. Hope everything is clear. Thanks!

Upvotes: 1

Views: 1452

Answers (1)

eran otzap
eran otzap

Reputation: 12533

The order in which the triggers are declared is the problem .

 <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
         <Setter Property="Background" Value="#FF3EC8C6" />    
      </Trigger>
      <Trigger Property="IsPressed" Value="True">
         <Setter Property="Background" Value="Transparent" />        
      </Trigger>
      <Trigger Property="IsFocused" Value="True">
         <Setter Property="Background" Value="Transparent" />
      </Trigger>
 </ControlTemplate.Triggers>

The IsMouseOver Occurs by right after it the IsFocused kicks in and makes the background transparent.

Try it like this :

      <ControlTemplate.Triggers>
     <Trigger Property="IsFocused" Value="True">
         <Setter Property="Background" Value="#FF3EC8C6" />                    </Trigger>
      <Trigger Property="IsPressed" Value="True">
         <Setter Property="Background" Value="Transparent" />        
      </Trigger>
      <Trigger Property="IsMouseOver" Value="True">
         <Setter Property="Background" Value="Transparent" />
      </Trigger>
 </ControlTemplate.Triggers>       

Upvotes: 1

Related Questions