A strange conflict between the XAML buttons IsMouseOver and IsEnabled causes active work when disconnected

In my application, I have my own style and button template that have a couple of states based on XAML triggers:

<Style x:Key="UniversalButton" TargetType="{x:Type Button}">
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Background" Value="#FFD7D7D7" />
    <Setter Property="BorderBrush" Value="#FF999999" />
    <Setter Property="Foreground" Value="#FF666666" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ControlTemplate.Resources>
                    <Style x:Key="ButtonBorder" TargetType="{x:Type Border}">
                        <Setter Property="CornerRadius" Value="3" />
                        <Setter Property="BorderThickness" Value="0,0,0,2" />
                        <Setter Property="SnapsToDevicePixels" Value="True" />
                    </Style>

                    <Style x:Key="ButtonContent" TargetType="{x:Type ContentPresenter}">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Setter Property="TextBlock.TextAlignment" Value="Center" />
                        <Setter Property="Margin" Value="8,7,8,8" />
                        <Setter Property="RecognizesAccessKey" Value="True" />
                        <Setter Property="SnapsToDevicePixels" Value="True" />
                    </Style>
                </ControlTemplate.Resources>
                <ControlTemplate.Triggers>
                    <Trigger Property="ContextMenu.IsOpen" Value="True">
                        <Setter Property="Opacity" Value="0.8" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsEnabled" Value="True" />
                        </MultiTrigger.Conditions>
                        <MultiTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.8" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.EnterActions>
                        <MultiTrigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </MultiTrigger.ExitActions>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.6" />
                    </Trigger>
                </ControlTemplate.Triggers>
                <Border Style="{DynamicResource ButtonBorder}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Padding="{TemplateBinding Padding}"
                        Opacity="{TemplateBinding Opacity}">
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <ContentPresenter Style="{DynamicResource ButtonContent}"
                                          TextBlock.FontWeight="{TemplateBinding FontWeight}"
                                          TextBlock.Foreground="{TemplateBinding Foreground}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

A particular instance of this button has a command CanExecutebased on the selection ItemsControlin the view. When I switch my selection, including those designed to disable the button, everything works fine. The button becomes translucent, as I requested in the XAML trigger for IsEnabled.

The problem starts when I click on the button, activating the storyboards above. After that, changing the selection to the state in which the button is disabled leaves the button completely opaque, regardless of the IsEnabled/ state CanExecute.

, CanExecute, , IsMouseOver. XAML, , .

!

+4
1

, WPF. ( ) FillBehavior FillBehavior.HoldEnd. ( Opacity) . , , ( , , codebehind, ).

Trigger ( 7) . , , ( ). , Duration :

<Trigger Property="ContextMenu.IsOpen" Value="True">        
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
               <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                                To=".8" Duration="0:0:0"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
    <!-- similar for ExitActions --> 

</Trigger>

<!--  -->

<Trigger Property="IsEnabled" Value="False">
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
               <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                                To=".6" Duration="0:0:0"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
    <!-- similar for ExitActions --> 

</Trigger>

Storyboard . , , , ...

PS: XAML. XAML. , , .

+1

All Articles