gpt4 book ai didi

wpf - XAML IsMouseOver 和 IsEnabled 的奇怪冲突导致按钮在禁用时显示为事件状态

转载 作者:行者123 更新时间:2023-12-01 05:09:34 26 4
gpt4 key购买 nike

在我的应用程序中,我有一个自定义按钮样式和模板,它有几个基于 XAML 触发器的状态:

<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>

相关按钮的特定实例有一个带有 CanExecute 的命令。基于 ItemsControl 的选择在 View 中。当我切换我的选择时,包括那些旨在禁用按钮的选择,一切正常。正如我在 IsEnabled 的 XAML 触发器中所要求的那样,按钮变为半透明。 .

当我将鼠标悬停在按钮上时,问题就开始了,激活上面的 Storyboard。发生这种情况后,将选择更改为禁用按钮的状态会使按钮完全不透明,而不管 IsEnabled 的状态如何。/ CanExecute .

通过广泛的调试,我确定问题不是由 CanExecute 引起的。如前所述,它必须链接到 IsMouseOver扳机。在试图解决这个冲突时,我已经用尽了我所有的 XAML 知识,我将非常感谢您的帮助。

提前致谢!

最佳答案

我认为问题是由 Dependency Property value precedence 引起的在 WPF 中。在这种情况下,动画(时间轴)具有默认值 FillBehaviorFillBehavior.HoldEnd .动画完成后,所有更改值(Opacity 属性)的尝试都应该具有更高的优先级。从链接中您可以看到只有系统强制该值可以覆盖基于动画的值(即使是本地设置,例如直接在代码隐藏中分配也无法覆盖此值)。

这就是为什么您的模板触发器(其优先级为 7)无法覆盖当前动画值的原因。对此有一些解决方案,但我认为您应该在触发器中使用所有 Storyboard(而不是使用 setter)。如果你不想要任何动画,只需设置 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这种方式也有利于维护。那是当您想为属性值更改添加一些动画时,那么您已经有了 Storyboard,您可以添加更多选项或动画...

PS:不要害怕很长的 XAML 代码。这是 XAML 代码的本质和特征。然而,作为一种权衡,我们对行为和效果有更多的控制。

关于wpf - XAML IsMouseOver 和 IsEnabled 的奇怪冲突导致按钮在禁用时显示为事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com